import numpy as np
#随机生成2行4列值在(0,1)内的数组
a = np.random.random((2,4))
print(a)
"""
[[0.05932697 0.26692883 0.80273429 0.56648727]
[0.88395289 0.53249124 0.63557664 0.11671337]]
"""
print(np.sum(a))
print(np.min(a))
print(np.max(a))
"""
3.864211483770041
0.05932696538694493
0.8839528875161482
"""
#axis=0在列上操作
print(np.sum(a,axis=0))
#axis=1在行上操作
print(np.min(a,axis=1))
"""
[0.89938846 1.00042835 1.55796798 1.91173002]
[0.58926711 0.19984479]
"""
#寻找数组内最大值索引和最小值索引
A = np.arange(2,14).reshape((3,4))
print(np.argmin(A))
print(np.argmax(A))
"""
0
11
"""
#均值
print(np.mean(A))
print(A.mean())
print(np.average(A))
"""
7.5
7.5
7.5
"""
#中位数
print(np.median(A))
"""
7.5
"""
#逐步累加
print(A)
print(np.cumsum(A))
"""
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[ 2 5 9 14 20 27 35 44 54 65 77 90]
"""
#累差
print(np.diff(A))
"""
[[1 1 1]
[1 1 1]
[1 1 1]]
"""
#排序,行内排序
A = [[2,4,3],[7,4,9],[1,2,2]]
print(A)
print(np.sort(A))
"""
[[2, 4, 3], [7, 4, 9], [1, 2, 2]]
[[2 3 4]
[4 7 9]
[1 2 2]]
"""
#矩阵转置
print(np.transpose(A))
"""
[[2 7 1]
[4 4 2]
[3 9 2]]
"""
A = np.arange(2,14).reshape((3,4))
print(A)
print(A.T)
"""
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
"""
#矩阵积
print(A.T.dot(A))
"""
[[140 158 176 194]
[158 179 200 221]
[176 200 224 248]
[194 221 248 275]]
"""
#将大于11的数改为11,小于4的数改为4
print(np.clip(A,4,11))
"""
[[ 4 4 4 5]
[ 6 7 8 9]
[10 11 11 11]]
"""
#索引
print(A)
print(A[1])
print(A[1][1])
print(A[1,1])
"""
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[6 7 8 9]
7
7
"""
#索引下标为2行和下标为3的列
print(A[2,:])
print(A[:,3])
"""
[10 11 12 13]
[ 5 9 13]
"""
#所引第二行第一个是个数到第二个数
print(A[1,0:2])
"""
[6 7]
"""
for row in A:
print(row)
"""
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]
"""
#利用矩阵转置输出列
for col in A.T:
print(col)
"""
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
"""
#将多维矩阵转换成一维
print(A.flatten())
"""
[ 2 3 4 5 6 7 8 9 10 11 12 13]
"""
for item in A.flat:
print(item)
"""
2
3
4
5
6
7
8
9
10
11
12
13
"""
B站视频:https://www.bilibili.com/video/av16378934/?p=1