初步认识numpy的属性 ndim shape size
# 创建numpy数组
import numpy as np
array = np.array([[1,2,3],
[4,5,6]])
print(array)
print("number of dim", array.ndim) # 维度
print("shape", array.shape) # 2 * 3
print("size", array.size) # 6
[[1 2 3]
[4 5 6]]
number of dim 2
shape (2, 3)
size 6
认识numpy的数据类型
a = np.array([2, 23, 4], dtype=np.int64)
print(a)
print(a.dtype)
a2 = np.array([5, 6,7 ], dtype = np.float32)
print(a2.dtype)
[ 2 23 4]
int64
float32
常见的生成array的方式
a_zero = np.zeros( (3, 4) ) # 全0, 形状要用()括起来
a_zero
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
a_ones = np.ones( (3, 4), dtype = np.int16);
a_ones
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=int16)
a_empty = np.empty( (3,4) )
a_empty
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
a_range = np.arange(10, 20, 2);
a_range
array([10, 12, 14, 16, 18])
a_reshape = np.arange(12).reshape( (3, 4) )
a_reshape
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
a_line = np.linspace(0, 10, 5).reshape( (5,1) )
a_line
array([[ 0. ],
[ 2.5],
[ 5. ],
[ 7.5],
[ 10. ]])
常见的numpy运算
# numpy的运算
a = np.array([10, 20, 30, 40]);
b = np.arange(4);
print(a, b)
print(a + b)
print(a * b)
print(b ** 2) #平方
print(10 * np.sin(a)) #sin 等数学运算
[10 20 30 40] [0 1 2 3]
[10 21 32 43]
[ 0 20 60 120]
[0 1 4 9]
[-5.44021111 9.12945251 -9.88031624 7.4511316 ]
print(b < 3) # 判断mask
[ True True True False]
print(b == 2) #判断mask 是否相等
[False False True False]
#矩阵计算
a_mat = np.array([[1, 1],[0, 1]]);
b_mat = np.arange(4).reshape((2,2));
print(a_mat)
print(b_mat)
print(a_mat * b_mat)
print(np.dot(a_mat, b_mat)) #矩阵计算
print(a_mat.dot(b_mat))
[[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.max(a))
print(np.min(a))
[[ 0.74260727 0.20295659 0.00618381 0.47263097]
[ 0.43585519 0.73091641 0.09287091 0.59677618]]
3.28079732304
0.742607271838
0.00618381462723
# axis 按轴计算
print(np.sum(a, axis = 0)) #沿着y轴 求和
print(np.max(a, axis = 1)) # 沿着x轴 求最大
[ 1.17846246 0.933873 0.09905472 1.06940714]
[ 0.74260727 0.73091641]
A = np.arange(2, 14).reshape( (2, 6))
print(A)
[[ 2 3 4 5 6 7]
[ 8 9 10 11 12 13]]
np.argmin(A) #最小值索引
np.argmax(A) #最大值索引
11
np.mean(A) #平均值
7.5
A.mean()
7.5
np.median(A) #中位数
7.5
np.cumsum(A) #累加
array([ 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90], dtype=int32)
np.diff(A) #累差
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
np.nonzero(A) #非零的数字
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int64),
array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], dtype=int64))
a_withzero = np.arange(-3, 5).reshape( (2, 4))
print(a_withzero)
np.nonzero(a_withzero) # 返回非0的位置 用两个array的形式返回
[[-3 -2 -1 0]
[ 1 2 3 4]]
(array([0, 0, 0, 1, 1, 1, 1], dtype=int64),
array([0, 1, 2, 0, 1, 2, 3], dtype=int64))
print(np.sort(A))
[[ 2 3 4 5 6 7]
[ 8 9 10 11 12 13]]
b_desc = np.arange(9, -1, -1); # 指定步长 linspace是指定步数
print(b_desc)
print(np.sort(b_desc))
[9 8 7 6 5 4 3 2 1 0]
[0 1 2 3 4 5 6 7 8 9]
print(np.transpose(b_desc))
[9 8 7 6 5 4 3 2 1 0]
print(b_desc)
[9 8 7 6 5 4 3 2 1 0]
a_2d = np.arange(2, 14).reshape( (6, 2))
print(a_2d)
print(a_2d.T)
[[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]]
[[ 2 4 6 8 10 12]
[ 3 5 7 9 11 13]]
a_ran = np.random.random( (3,4))
print(a_ran)
print(np.sort(a_ran))
print(np.sort(a_ran, axis = 0))
print(np.sort(a_ran, axis=1))
[[ 0.77974552 0.87391665 0.42413729 0.54764486]
[ 0.70079738 0.73673138 0.11842954 0.40184359]
[ 0.52657421 0.85092361 0.06880996 0.76002899]]
[[ 0.42413729 0.54764486 0.77974552 0.87391665]
[ 0.11842954 0.40184359 0.70079738 0.73673138]
[ 0.06880996 0.52657421 0.76002899 0.85092361]]
[[ 0.52657421 0.73673138 0.06880996 0.40184359]
[ 0.70079738 0.85092361 0.11842954 0.54764486]
[ 0.77974552 0.87391665 0.42413729 0.76002899]]
[[ 0.42413729 0.54764486 0.77974552 0.87391665]
[ 0.11842954 0.40184359 0.70079738 0.73673138]
[ 0.06880996 0.52657421 0.76002899 0.85092361]]