记住:numpy的操作都是按元素进行的,即elementwise!
elementwise
>>> import numpy as np
>>> a = np.arange(10).reshape(2,5)
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> b = a + 1
>>> b
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
>>> c = a * 2
>>> c
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18]])
>>> b - c
array([[ 1, 0, -1, -2, -3],
[-4, -5, -6, -7, -8]])
>>> a ** 2
array([[ 0, 1, 4, 9, 16],
[25, 36, 49, 64, 81]], dtype=int32)
>>> a < 5
array([[ True, True, True, True, True],
[False, False, False, False, False]])
>>> np.sin(a)
array([[ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ],
[-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849]])
矩阵相乘
>>> a = np.arange(25).reshape(5,5)
>>> b = np.arange(25).reshape(5,5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>> b
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>> a*b
array([[ 0, 1, 4, 9, 16],
[ 25, 36, 49, 64, 81],
[100, 121, 144, 169, 196],
[225, 256, 289, 324, 361],
[400, 441, 484, 529, 576]])
>>> a@b
array([[ 150, 160, 170, 180, 190],
[ 400, 435, 470, 505, 540],
[ 650, 710, 770, 830, 890],
[ 900, 985, 1070, 1155, 1240],
[1150, 1260, 1370, 1480, 1590]])
>>> np.dot(a,b)
array([[ 150, 160, 170, 180, 190],
[ 400, 435, 470, 505, 540],
[ 650, 710, 770, 830, 890],
[ 900, 985, 1070, 1155, 1240],
[1150, 1260, 1370, 1480, 1590]])
直接用*,表示elementwise的乘法,这种乘法叫Hadamard Product。
用@或者np.dot函数,都是常规的矩阵乘法。
如何不创建新的numpy.array
当计算结果是一个numpy.array时,默认情况,是创建一个新的array。而如果表达式使用类似于 += or *= 这样的符号,就不会创建新的array,就地修改原来的。
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>> id(a)
3039556576
>>> a = a + 1
>>> id(a)
3039556776
>>>
>>> a += 1
>>> id(a)
3039556776
看id函数的结果。
upcasting
When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).
简单的理解,当int和float一起计算时,结果就是float,因为float更精确。
sum, min, max
这些常用的简单的计算函数,numpy都有,熟悉一下就不用想着自己去写了。
>>> a = np.arange(10).reshape(2,5)
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> a.sum()
45
>>> a.min()
0
>>> a.max()
9
>>>
>>> a[1]
array([5, 6, 7, 8, 9])
>>> a[1].sum()
35
>>> a[1].min()
5
>>> a[1].max()
9
>>>
>>> a.sum(axis=1)
array([10, 35])
>>> a.min(axis=1)
array([0, 5])
>>> a.max(axis=1)
array([4, 9])
>>>
>>> a.sum(axis=0)
array([ 5, 7, 9, 11, 13])
>>> a.min(axis=0)
array([0, 1, 2, 3, 4])
>>> a.max(axis=0)
array([5, 6, 7, 8, 9])
axis=1时,就是在按row计算。
而axis=0时,就是在按column计算。你可以想象第1维度上有2个点,按这两个点计算,就是axis=0,由于每个点内包含5个小点,计算结果就是这对应的5个小点的计算结果。
ufunc
numpy提供一下常见的数学函数,比如sin,cos,exp等等,这些函数被称为Universal Function。可直接对array进行操作,当然也是elementwise,结果是一个新的array。
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.sin(a)
array([[ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ],
[-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849]])
>>> np.cos(a)
array([[ 1. , 0.54030231, -0.41614684, -0.9899925 , -0.65364362],
[ 0.28366219, 0.96017029, 0.75390225, -0.14550003, -0.91113026]])
>>> np.exp(a)
array([[1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
5.45981500e+01],
[1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,
8.10308393e+03]])
>>> np.tan(a)
array([[ 0. , 1.55740772, -2.18503986, -0.14254654, 1.15782128],
[-3.38051501, -0.29100619, 0.87144798, -6.79971146, -0.45231566]])
>>> np.log(a)
:1: RuntimeWarning: divide by zero encountered in log
array([[ -inf, 0. , 0.69314718, 1.09861229, 1.38629436],
[1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458]])
-- EOF --