numpy基本用法


创建矩阵、查看维度、形状和元素数量

import numpy as np

array = [[1,2,3],
         [4,5,6]]
array = np.array(array)
print(array)
print('number of dim: ', array.ndim)
print('shape: ', array.shape)
print('size: ', array.size)
[[1 2 3]
 [4 5 6]]
number of dim:  2
shape:  (2, 3)
size:  6

numpy数据类型

dtype(np.int64、int32、float64等等)

array = np.array([[1,2,3],
          [4,5,6]], dtype=np.int64)
print(array)
[[1 2 3]
 [4 5 6]]

创建特殊numpy矩阵

np.zeros((2,3))
array([[0., 0., 0.],
       [0., 0., 0.]])
np.ones((2,3), dtype=int)
array([[1, 1, 1],
       [1, 1, 1]])
np.random.random((2,3))
array([[0.22489044, 0.92831268, 0.81584165],
       [0.9038033 , 0.03581653, 0.41887279]])

在未创建数组之前调用此方法,元素的值接近于零;反之为0;若数据类型为int,则为随机数

import numpy as np
np.empty((2,3))
array([[3.56038640e-307, 3.33774490e-307, 1.44635573e-307],
       [9.34598926e-307, 7.56597091e-307, 1.78019625e-306]])

numpy生成range矩阵、等差、等比矩阵、reshape

np.arange(12)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
np.arange(12).reshape(3,4)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
np.linspace(0, 20, 5, dtype=int)
np.logspace(1.0,  2.0, 10) 
array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,
        27.82559402,  35.93813664,  46.41588834,  59.94842503,
        77.42636827, 100.        ])
np.linspace(0, 20, 24, dtype=int).reshape(2,3,4)
array([[[ 0,  0,  1,  2],
        [ 3,  4,  5,  6],
        [ 6,  7,  8,  9]],

       [[10, 11, 12, 13],
        [13, 14, 15, 16],
        [17, 18, 19, 20]]])

矩阵的计算

矩阵元素的加减乘除

a = np.array([[1,2],
              [3,4]])
b = np.arange(4).reshape(2,2)
# c = a+b
# c = a-b
c = a*b
# c = a/b
print(a)
print(b)
print('------')
print(c)
[[1 2]
 [3 4]]
[[0 1]
 [2 3]]
------
[[       inf 2.        ]
 [1.5        1.33333333]]


<ipython-input-36-d4c67e1eaa24>:7: RuntimeWarning: divide by zero encountered in divide
  c = a/b

矩阵的函数sin、cos、boolean

np.sin(a)
np.cos(a)
array([[ 0.54030231, -0.41614684],
       [-0.9899925 , -0.65364362]])
print(a<=2)
[[ True  True]
 [False False]]
print(a==2)
[[False  True]
 [False False]]

矩阵的乘法

print(a)
print(b)
# np.dot(a,b)
a.dot(b)
[[1 2]
 [3 4]]
[[0 1]
 [2 3]]





array([[ 4,  7],
       [ 8, 15]])

矩阵sum、max、min

r = np.random.random((2,3))
print(r)
[[0.29205341 0.83460167 0.70298406]
 [0.65505704 0.93624025 0.51601126]]
print(np.sum(r))
print(np.max(r))
print(np.min(r))
5.862923572296049
0.9865082934926919
0.05031000334319313
print(np.sum(r, axis=0))
print(np.max(r, axis=1))
print(np.min(r, axis=0))
[0.94711045 1.77084192 1.21899532]
[0.83460167 0.93624025]
[0.29205341 0.83460167 0.51601126]

矩阵内部计算

计算元素值最大索引和最小索引

import numpy as np

A = np.arange(2,14).reshape(3,4)
print(A)
print(np.argmax(A))
print(np.argmin(A))
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
11
0

计算矩阵均值,中位数,累加,相差,输出非零元素坐标

print(A)
print(np.mean(A, axis=0))
# print(A.mean())
# print(np.average(A))
print(np.median(A))
print(np.cumsum(A))
print(np.diff(A))
print(np.nonzero(A))
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
[6. 7. 8. 9.]
7.5
[ 2  5  9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))

排序,转置矩阵

B = np.arange(14,2,-1).reshape(3,4)
print(B)
print(np.sort(B))
# print(np.transpose(B))
print(B.T)

print((B.T).dot(B))
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[332 302 272 242]
 [302 275 248 221]
 [272 248 224 200]
 [242 221 200 179]]

截取矩阵(限定下限和上限)`

print(A)
# print(A.clip(5, 10))
print(np.clip(A, 5, 10))
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
[[ 5  5  5  5]
 [ 6  7  8  9]
 [10 10 10 10]]

矩阵的索引

索引

import numpy as np

A = np.arange(2,14).reshape(3,4)
B = np.arange(2,14)

print(A)
print(B)
print(A[2], B[2])
print(A[1][2], A[1,2])
print(A[1][2:], A[1,2:])
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
[ 2  3  4  5  6  7  8  9 10 11 12 13]
[10 11 12 13] 4
8 8
[8 9] [8 9]

迭代

print(A)
print("-----------")
for row in A:
    print(row)

print("-----------")
for col in A.T:
    print(col)
print("-----------")
print(A.flat)
print(A.flatten())
for item in A.flat:
    print(item)
print("-----------")
for item in A.flatten():
    print(item)
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
-----------
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]
-----------
[ 2  6 10]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
-----------
<numpy.flatiter object at 0x000001DE3F7647D0>
[ 2  3  4  5  6  7  8  9 10 11 12 13]
2
3
4
5
6
7
8
9
10
11
12
13
-----------
2
3
4
5
6
7
8
9
10
11
12
13

矩阵的合并

A = np.array([1,1,1])
B = np.array([2,2,2])

C = np.vstack((A,B)) # vertical stack
D = np.hstack((A,B)) # horizontal stack
print(C)
print(D)
print(C.shape, D.shape)
[[1 1 1]
 [2 2 2]]
[1 1 1 2 2 2]
(2, 3) (6,)

加维度

nA = np.array([1,1,1])[:,np.newaxis]
nB = np.array([1,1,1])[np.newaxis,:]
print(A)
print(nA)
print(nB)
print(nA.shape, nB.shape)
print(np.vstack((nA, nA)))
print(np.hstack((nA, nA)))
[1 1 1]
[[1]
 [1]
 [1]]
[[1 1 1]]
(3, 1) (1, 3)
[[1]
 [1]
 [1]
 [1]
 [1]
 [1]]
[[1 1]
 [1 1]
 [1 1]]
C = np.concatenate((nA,nA,nA,nA), axis = 1)
print(C)
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
x = [[1,1,1],
     [1,1,1]]
y = [[2,2,2],
     [2,2,2]]
X = np.array(x)
Y = np.array(y)
z = np.vstack((X,Y))
print(z)
z = np.hstack((X,Y))
print(z)
print(np.concatenate((X,Y,Y,X),axis=0))
print(np.concatenate((X,Y,Y,X),axis=1))
[[1 1 1]
 [1 1 1]
 [2 2 2]
 [2 2 2]]
[[1 1 1 2 2 2]
 [1 1 1 2 2 2]]
[[1 1 1]
 [1 1 1]
 [2 2 2]
 [2 2 2]
 [2 2 2]
 [2 2 2]
 [1 1 1]
 [1 1 1]]
[[1 1 1 2 2 2 2 2 2 1 1 1]
 [1 1 1 2 2 2 2 2 2 1 1 1]]

分割

A = np.arange(12).reshape(3,4)
print(A)
print(np.split(A,3,axis=0))
print(np.split(A,4,axis=1))
print(np.array_split(A,3,axis=1))
print(np.vsplit(A,3))
print(np.hsplit(A,4))
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0],
       [4],
       [8]]), array([[1],
       [5],
       [9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0],
       [4],
       [8]]), array([[1],
       [5],
       [9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]

拷贝

a = np.array([1,2,3,4])
b = a
c = b
a[0] = 11
print(c)
print(c is a)
print("---------")
a = np.array([1,2,3,4])
c = a
c = a.copy()
a[1] = 22
print(c)
[11  2  3  4]
True
---------
[1 2 3 4]