python 之 numpy 总结

参考链接:莫烦python https://mofanpy.com/tutorials/data-manipulation/np-pd/

import numpy as np

numpy 属性

array = np.array([[1,2,3],
                 [2,3,4]])
print(array)
print("number of dim:"array.ndim)
print("shape",array.shape)
print("size",array.size) # 有多少个元素
[[1 2 3]
 [2 3 4]]
number of dim: 2
shape (2, 3)
size 6

创建 array

a = np.array([2,23,4])
print(a)
[ 2 23  4]
a = np.array([2,23,4],dtype=np.int64) #int float16 32 64
print(a.dtype)
int64
a = np.arange(10,20,2)  #range类似
print(a)
[10 12 14 16 18]
a = np.arange(12).reshape((3,4))
print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
# 定义矩阵
a = np.array([[3,4,6],
             [7,1,8]])
print(a)
[[3 4 6]
 [7 1 8]]
a = np.zeros((3,4)) #ones empty
print(a)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
#生成线段
a = np.linspace(1,10,5) #5段
print(a)
[ 1.    3.25  5.5   7.75 10.  ]
a = np.linspace(1,10,6).reshape((2,3))
print(a)
[[ 1.   2.8  4.6]
 [ 6.4  8.2 10. ]]

numpy 基础运算

a = np.array([10,20,30,40])
b = np.arange(4)
print(a,b)
c = a - b # + 逐个相加减法
print(c)
[10 20 30 40] [0 1 2 3]
[10 19 28 37]
a = np.array([10,20,30,40])
b = np.arange(4)
print(a,b)  # / 逐个相乘除法
c = a * b
print(c)
[10 20 30 40] [0 1 2 3]
[  0  20  60 120]
b = np.arange(4)
print(b)  # / 逐个相乘除法
c = b ** 2 # 平方是 **
print(c)
[0 1 2 3]
[0 1 4 9]
a = np.array([10,20,30,40])
c = 10*np.sin(a) # 每个值sin以后再*10  cos tan
print(c)
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
np.sin(np.pi/6) # 1/2
0.49999999999999994
b = np.arange(4)
print(b)http://localhost:8888/notebooks/LearnArray.ipynb#%E7%9F%A9%E9%98%B5%E8%BF%90%E7%AE%97
print(b<3) # == >=
[0 1 2 3]
[ True  True  True False]

矩阵运算

a = np.array([[1,1],
             [0,1]])
b = np.arange(4).reshape((2,2))
print(a)
print(b)

print("逐个相乘")
print(a*b) #逐个相乘

print("矩阵相乘")
print(np.dot(a,b)) #矩阵相乘
print(a.dot(b))
[[1 1]
 [0 1]]
[[0 1]
 [2 3]]
逐个相乘
[[0 1]
 [0 3]]
矩阵相乘
[[2 4]
 [2 3]]
[[2 4]
 [2 3]]
a = np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.min(a))
print(np.max(a))
[[0.75043705 0.64915184 0.90982739 0.95481296]
 [0.10239917 0.05245467 0.05292892 0.15421724]]
3.6262292489095636
0.05245467473374732
0.9548129643015092
a = np.random.random((2,4))
print(a,end="\n\n")
print(np.sum(a,axis=1)) #axis=1为行
[[0.24422292 0.75909185 0.77952272 0.45213038]
 [0.14313807 0.24731068 0.14017471 0.99728501]]

[2.23496787 1.52790848]
print(np.min(a,axis=0)) #axis=0为列
[0.14313807 0.24731068 0.14017471 0.45213038]

矩阵运算2

A = np.arange(2,14).reshape((3,4))
print(A,end="\n\n")

print(np.argmin(A)) #最小值的索引
print(np.argmax(A)) #最大值的索引
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]

0
11
print(A.mean()) #np.mean(A)
print("平均数",np.average(A)) #不能 A.average(A)
print("中位数",np.median(A))
7.5
平均数 7.5
中位数 7.5
print("逐步累加 cumsum",np.cumsum(A))
print("相邻差 diff",np.diff(A))
逐步累加 cumsum [ 2  5  9 14 20 27 35 44 54 65 77 90]
相邻差 diff [[1 1 1]
 [1 1 1]
 [1 1 1]]
print("非0的数",np.nonzero(A)) #先输出行数,在输出列数
非0的数 (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))
A = np.arange(14,2,-1).reshape((3,4))
print(A,end="\n\n")
print(np.sort(A)) #逐行排序
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]

[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
A = np.arange(14,2,-1).reshape((3,4))
print(A,end="\n\n")
print(np.transpose(A))
print(A.T)
print((A.T).dot(A)) #实对称
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]

[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[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]]
A = np.arange(14,2,-1).reshape((3,4))
print(A,end="\n\n")
print(np.clip(A,5,11)) #所有小于 5 的数变成 5,所有大于 11 的数变成 11
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]

[[11 11 11 11]
 [10  9  8  7]
 [ 6  5  5  5]]
A = np.arange(14,2,-1).reshape((3,4))
print(A,end="\n\n")
print("计算每列平均数",np.mean(A,axis=0)) # 计算每列平均数
print("计算每行平均数",np.mean(A,axis=1)) # 计算每行平均数
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]

计算每列平均数 [10.  9.  8.  7.]
计算每行平均数 [12.5  8.5  4.5]

numpy索引

import numpy as np

A = np.arange(3,15)
print(A)
print(A[3])
[ 3  4  5  6  7  8  9 10 11 12 13 14]
6
A = np.arange(3,15).reshape(3,4)
print(A)
print(A[1][1])
print(A[2,1])
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
8
12
print(A[2,:])
print(A[:,1])
print(A[0,1:4])
[11 12 13 14]
[ 4  8 12]
[4 5 6
# 输出列,用T,妙啊
for col in A.T:
    print(col)
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
# 输出矩阵中的每一个值
print(A.flatten())
for item in A.flat:
    print(item)
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14

numpy array 合并

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

C = np.vstack((A,B))
print(C)
print(A.shape,C.shape)
[[1 1 1]
 [2 2 2]]
(3,) (2, 3)
D = np.hstack((A,B))
print(D)
print(A.shape,D.shape)
[1 1 1 2 2 2]
(3,) (6,)
# 变成 [[1],[1],[1]]
E = A[np.newaxis,:] # 在行上加了一个维度
print(E) 
print(E.shape,end="\n\n") 

F = A[:,np.newaxis]# 在列上加了一个维度
print(F)
print(F.shape) 
[[1 1 1]]
(1, 3)

[[1]
 [1]
 [1]]
(3, 1)
A = np.array([1,1,1])[:,np.newaxis]
print(A,end="\n\n")
B = np.array([2,2,2])[:,np.newaxis]
print(B,end="\n\n")
G = np.hstack((A,B))
print(G)
[[1]
 [1]
 [1]]

[[2]
 [2]
 [2]]

[[1 2]
 [1 2]
 [1 2]]
C = np.concatenate((A,B,B,A),axis=0) #上下合并
print(C,end="\n\n")
C = np.concatenate((A,B,B,A),axis=1) #左右合并
print(C)
[[1]
 [1]
 [1]
 [2]
 [2]
 [2]
 [2]
 [2]
 [2]
 [1]
 [1]
 [1]]

[[1 2 2 1]
 [1 2 2 1]
 [1 2 2 1]]

numpy array 分割

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

numpy 深拷贝和浅拷贝

a = np.arange(4)
b = a
c = b

print(a)
print(b)
a[0] = 11
print(a)
print(b) # b也跟着变了,用的是同一个内存地址
print(c is a)
[0 1 2 3]
[0 1 2 3]
[11  1  2  3]
[11  1  2  3]
True
b = a.copy() # 深拷贝
print(a)
print(b)

a[0]=88
print(a)
print(b)
[11  1  2  3]
[11  1  2  3]
[88  1  2  3]
[11  1  2  3]
posted on 2021-08-17 16:12  蔡军帅  阅读(86)  评论(0编辑  收藏  举报