avatar

目录
numpy 高级命令速查表

此notebook复现以下链接.
Numpy快速上手指南 —- 进阶篇

python
1
2
import numpy as np
import math

高级索引技巧

一维数组索引

python
1
2
3
a = np.arange(12)**2
i = np.array([1,1,3,8,5])
a[i]
array([ 1,  1,  9, 64, 25], dtype=int32)
python
1
2
j = np.array( [ [ 3, 4], [ 9, 7 ] ] )         # a bidimensional array of indices
a[j]
array([[ 9, 16],
       [81, 49]], dtype=int32)
python
1
2
3
4
5
6
7
8
palette = np.array( [ [0,0,0],                # 黑色
[255,0,0], # 红色
[0,255,0], # 绿色
[0,0,255], # 蓝色
[255,255,255] ] ) # 白色
image = np.array( [ [ 0, 1, 2, 0 ], # each value corresponds to a color in the palette
[ 0, 3, 4, 0 ] ] )
palette[image]
array([[[  0,   0,   0],
        [255,   0,   0],
        [  0, 255,   0],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0, 255],
        [255, 255, 255],
        [  0,   0,   0]]])
python
1
2
a = np.arange(12).reshape(3,4)
a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

多维数组索引

多维的索引数组也是可以的.每一维的数组必须有相同的形状.

python
1
2
i = np.array( [ [0,1],                        # indices for the first dim of a
[1,2] ] )
python
1
2
j = np.array( [ [2,1],                        # indices for the second dim
[3,3] ] )
python
1
a[i,j]
array([[ 2,  5],
       [ 7, 11]])
python
1
a[i,j] #broadcast
array([[ 2,  5],
       [ 7, 11]])
python
1
a[:,j]
array([[[ 2,  1],
        [ 3,  3]],

       [[ 6,  5],
        [ 7,  7]],

       [[10,  9],
        [11, 11]]])

把i,j放在一个tuple里当成索引也可以

python
1
2
l = (i,j)
a[l]
array([[ 2,  5],
       [ 7, 11]])

例子: 寻找多个数列的最大值

python
1
2
3
time = np.linspace(20, 145, 5)                 # time scale
data = np.sin(np.arange(20)).reshape(5,4) # 4 time-dependent series
time
array([ 20.  ,  51.25,  82.5 , 113.75, 145.  ])
python
1
data
array([[ 0.        ,  0.84147098,  0.90929743,  0.14112001],
       [-0.7568025 , -0.95892427, -0.2794155 ,  0.6569866 ],
       [ 0.98935825,  0.41211849, -0.54402111, -0.99999021],
       [-0.53657292,  0.42016704,  0.99060736,  0.65028784],
       [-0.28790332, -0.96139749, -0.75098725,  0.14987721]])
python
1
2
ind = data.argmax(axis=0)                   # index of the maxima for each series
ind
array([2, 0, 3, 1], dtype=int64)
python
1
2
data_max = data[ind, range(data.shape[1])] # => data[ind[0],0], data[ind[1],1]...
data_max
array([0.98935825, 0.84147098, 0.99060736, 0.6569866 ])
python
1
np.all(data_max == data.max(axis=0))
True

例子: 数组索引作为目标赋值

python
1
a = np.arange(5)
python
1
a
array([0, 1, 2, 3, 4])
python
1
2
a[[1,2,3]] = 0
a
array([0, 0, 0, 0, 4])

例子:当一个索引列表包含重复时,赋值被多次完成,保留最后的值

python
1
2
3
a = np.arange(5)
a[[0,0,2]]=[1,2,3]
a
array([2, 1, 3, 3, 4])

通过布尔数组索引

索引

python
1
2
3
a = np.arange(12).reshape(3,4)
b = a > 4
b # b is a boolean with a's shape
array([[False, False, False, False],
       [False,  True,  True,  True],
       [ True,  True,  True,  True]])

赋值

python
1
2
a[b] = 0    # All elements of 'a' higher than 4 become 0
a
array([[0, 1, 2, 3],
       [4, 0, 0, 0],
       [0, 0, 0, 0]])

多维布尔数组索引

python
1
2
a = np.arange(12).reshape(3,4)
a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
python
1
2
3
b1 = np.array([False,True,True])
b2 = np.array([True,False,True,False])
a[b1,:]
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
python
1
a[b1]
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
python
1
a[:,b2]
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])
python
1
a[b1,b2]
array([ 4, 10])

np.ix_索引

np.ix_返回每一维相互组合的索引

python
1
a = ap.arange(10).reshape(2, 5)
python
1
ixgrid = np.ix_([0, 1], [2, 4])
python
1
a[ixgrid]
array([[2, 4],
       [7, 9]])

高级例子, 用np.ix_来计算所有元素组合的结果

python
1
2
3
4
a = np.array([2,3,4,5])
b = np.array([8,5,4])
c = np.array([5,4,6,8,3])
ax,bx,cx = np.ix_(a,b,c)
python
1
ax
array([[[2]],

       [[3]],

       [[4]],

       [[5]]])
python
1
bx
array([[[8],
        [5],
        [4]]])
python
1
cx
array([[[5, 4, 6, 8, 3]]])
python
1
ax.shape, bx.shape, cx.shape
((4, 1, 1), (1, 3, 1), (1, 1, 5))
python
1
result = ax + bx * cx
python
1
result
array([[[42, 34, 50, 66, 26],
        [27, 22, 32, 42, 17],
        [22, 18, 26, 34, 14]],

       [[43, 35, 51, 67, 27],
        [28, 23, 33, 43, 18],
        [23, 19, 27, 35, 15]],

       [[44, 36, 52, 68, 28],
        [29, 24, 34, 44, 19],
        [24, 20, 28, 36, 16]],

       [[45, 37, 53, 69, 29],
        [30, 25, 35, 45, 20],
        [25, 21, 29, 37, 17]]])
python
1
result[3, 2, 4]
17
python
1
a[3] + b[2] * c[4]
17

线性代数

运算

python
1
2
a = np.array([[1.0, 2.0], [3.0, 4.0]])
print (a)
[[1. 2.]
 [3. 4.]]

转置

python
1
a.transpose()
Object `solve` not found.

逆矩阵

python
1
np.linalg.inv(a)
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

单位对角矩阵

python
1
np.eye(2)
array([[1., 0.],
       [0., 1.]])

点积

python
1
2
j = np.array([[0.0, -1.0], [1.0, 0.0]])
np.dot(j, j) # matrix product
array([[-1.,  0.],
       [ 0., -1.]])

对角数据和

python
1
a = np.array([[1.0, 2.0], [3.0, 4.0]])
python
1
np.trace(a)
5.0

矩阵点乘方程求解

python
1
2
3
a = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([[5.], [7.]])
np.linalg.solve(a, y)
array([[-3.],
       [ 4.]])

1(-3) + 24 = 3(-3) + 44 = 7

python
1
np.dot(np.array([[1.0, 2.0], [3.0, 4.0]]), np.array([[-3.], [ 4.]]))
array([[5.],
       [7.]])

特征值和特征向量

python
1
2
j = np.array([[0.0, -1.0], [1.0, 0.0]])
np.linalg.eig(j)
(array([0.+1.j, 0.-1.j]),
 array([[0.70710678+0.j        , 0.70710678-0.j        ],
        [0.        -0.70710678j, 0.        +0.70710678j]]))

矩阵

略. 参考 Numpy快速上手指南 —- 进阶篇


评论