算术运算符

>>> a = np.array([6,7,8,9])
>>> b = np.arange(4)
>>> c = a-b
>>> c
array([6, 6, 6, 6])
>>> b
array([0, 1, 2, 3])
>>> b**2
array([0, 1, 4, 9])
>>> b<1
array([ True, False, False, False])


矩阵计算

>>> a = np.array([[0,1], [2,3]])
>>> b = np.array([[2,3], [4,5]])
>>> a * b            # 对应元素直接做乘法
array([[ 0,  3],
[ 8, 15]])
>>> a @ b            # 矩阵运算
array([[ 4,  5],
[16, 21]])
>>> a.dot(b)
array([[ 4,  5],
[16, 21]])


修改数组的值

>>> a = np.ones((2,3), dtype=int)
>>> a
array([[1, 1, 1],
[1, 1, 1]])
>>> a *= 3
>>> a
array([[3, 3, 3],
[3, 3, 3]])

>>> b = np.random.random((2,3))
>>> b
array([[0.69130364, 0.78987813, 0.54995678],
[0.72684798, 0.60646457, 0.87405791]])

>>> b += a
>>> b
array([[3.69130364, 3.78987813, 3.54995678],
[3.72684798, 3.60646457, 3.87405791]])
>>> a += b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'


向上转型

>>> a = np.ones(3, dtype=np.int32)
>>> a
array([1, 1, 1], dtype=int32)
>>> b = np.linspace(0,pi,3)
>>> b
array([0.        , 1.57079633, 3.14159265])
>>> b.dtype.name
'float64'

>>> c = a+b
>>> c
array([1.        , 2.57079633, 4.14159265])
>>> c.dtype.name
'float64'

>>> d = np.exp(c*1j)
>>> d
array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,
-0.54030231-0.84147098j])
>>> d.dtype.name
'complex128'


一元操作

>>> a = np.random.random((2,3))
>>> a.sum()
3.5360788979749973
>>> a.min()
0.20503831331878253
>>> a.max()
0.8077413278893076


数轴

>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])
>>> a.sum(axis=0)           # 每一列的和
array([12, 15, 18, 21])
>>> a.sum(axis=1)           # 每一行的和
array([ 6, 22, 38])
>>> a.min(axis=0)           # 每一列的最小值
array([0, 1, 2, 3])
>>> a.max(axis=1)           # 每一行的最大值
array([ 3,  7, 11])
>>> a.cumsum(axis=0)        # cumulative sum along each row
array([[ 0,  1,  2,  3],
[ 4,  6,  8, 10],
[12, 15, 18, 21]])


basic-operations