基本操作
1、数组上的算术运算符会应用到 元素 级别
a = np.array( [20,30,40,50] )
print(10*np.sin(a))
print(a<35)
b = np.arange( 1,5 )
print(b**2)
c = a-b
print(c)
运行结果:
[ 9.12945251 -9.88031624 7.4511316 -2.62374854]
[ True True False False]
[ 1 4 9 16]
[19 28 37 46]
- 与许多矩阵语言不同,乘积运算符*在NumPy数组中按元素进行运算。矩阵乘积可以使用@运算符(在python> = 3.5中)或dot函数或方法执行
A = np.array( [[1,1],[0,1]] )
B = np.array( [[2,0],[3,4]] )
print("A * B:",A * B)#积运算符*在NumPy数组中按元素进行运算
#矩阵乘积可以使用@运算符(在python> = 3.5中)或dot函数或方法执行
print("A @ B:",A @ B)
print("A.dot(B):", A.dot(B))
运行结果:
A * B: [[2 0]
[0 4]]
A @ B: [[5 4]
[3 4]]
A.dot(B): [[5 4]
[3 4]]
3、某些操作(例如+=和 *=)会更直接更改被操作的矩阵数组而不会创建新矩阵数组。
a = np.ones((2,3), dtype=int)
a *= 3
print(a)
b = np.random.random((2,3))
print(b)
b += a
print(b)
运行结果:
[[3 3 3]
[3 3 3]]
[[0.35287533 0.56195619 0.74018439]
[0.5019062 0.56990613 0.35129924]]
[[3.35287533 3.56195619 3.74018439]
[3.5019062 3.56990613 3.35129924]]
常用一元操作
a = np.random.random((2,3))
print(a.sum())
print(a.max())
print(a.min())
#通过指定axis 参数,您可以沿数组的指定轴应用操作
b = np.arange(12).reshape(3,4)
print(b)
print(b.sum(axis=0))# sum of each column
print(b.sum(axis=1))# sum of each row
print(b.max(axis=0))# max of each column
print(b.max(axis=1))# max of each row
#按指定轴逐个累加
print(b.cumsum(axis=1)) # cumulative sum along each row
运行结果:
3.0334450999885023
0.8713568612575104
0.1267850937442485
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[12 15 18 21]
[ 6 22 38]
[ 8 9 10 11]
[ 3 7 11]
[[ 0 1 3 6]
[ 4 9 15 22]
[ 8 17 27 38]]
通函数
NumPy提供熟悉的数学函数,例如sin,cos和exp。在NumPy中,这些被称为“通函数”(ufunc)。在NumPy中,这些函数在数组上按元素进行运算,产生一个数组作为输出。
B = np.arange(3)
print(B)
print(np.exp(B))
print(np.sqrt(B))
C = np.array([2., -1., 4.])
print(np.add(B, C))
a = np.arange(10)**3
print(a)
运行结果:
[0 1 2]
[1. 2.71828183 7.3890561 ]
[0. 1. 1.41421356]
[2. 0. 6.]
[ 0 1 8 27 64 125 216 343 512 729]
索引、切片和迭代
a = np.arange(10)**3
print(a)#[ 0 1 8 27 64 125 216 343 512 729]
print("索引2位置的值",a[2])# 8
print("切片a[2:5]:",a[2:5])#包含前面,不包含后边 [ 8 27 64]
print("切片a[0:6:2]:",a[0:6:2])
print("切片a[:6:2]:",a[:6:2])
a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
print(a)
print('reversed a:',a[ : :-1])# reversed a
print("遍历a:",end="")
for i in a:
print(i,end=" ")
[ 0 1 8 27 64 125 216 343 512 729]
索引2位置的值 8
切片a[2:5]: [ 8 27 64]
切片a[0:6:2]: [ 0 8 64]
切片a[:6:2]: [ 0 8 64]
[-1000 1 -1000 27 -1000 125 216 343 512 729]
reversed a: [ 729 512 343 216 125 -1000 27 -1000 1 -1000]
遍历a:-1000 1 -1000 27 -1000 125 216 343 512 729
#多维的数组每个轴可以有一个索引。这些索引以逗号分隔的元组给出
def f(x,y):
return 10*x+y
b = np.fromfunction(f,(5,4),dtype=int)
print("b:",b)
print("b[2,3]:",b[2,3])
print("每行第二列b[ : ,1]:",b[ : ,1])# each row in the second column of b
print("第0行到第4行的第二列b[0:5, 1]):",b[0:5, 1])# each row in the second column of b
print("第二行和第三行的所有列b[1:3, : ]:",b[1:3, : ])# each column in the second and third row of b
#当提供的索引少于轴的数量时,缺失的索引被认为是完整的切片
print("最后一行的所有列:", b[-1])
b = np.fromfunction(f,(2,3),dtype=int)
print("b:",b)
#对多维数组进行 迭代(Iterating) 是相对于第一个轴完成的
print("打印b的所有行:")
for row in b:
print(row)
#但是,如果想要对数组中的每个元素执行操作,可以使用flat属性,该属性是数组的所有元素的迭代器
print("打印b的所有元素用flat:")
for element in b.flat:
print(element)
b: [[ 0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]]
b[2,3]: 23
每行第二列b[ : ,1]: [ 1 11 21 31 41]
第0行到第4行的第二列b[0:5, 1]): [ 1 11 21 31 41]
第二行和第三行的所有列b[1:3, : ]: [[10 11 12 13]
[20 21 22 23]]
最后一行的所有列: [40 41 42 43]
b: [[ 0 1 2]
[10 11 12]]
打印b的所有行:
[0 1 2]
[10 11 12]
打印b的所有元素用flat:
0
1
2
10
11
12
#索引、切片和迭代
#三个点( ... )表示产生完整索引元组所需的冒号。例如,如果 x 是rank为的5数组(即,它具有5个轴),则:
# x[1,2,...] 相当于 x[1,2,:,:,:],
# x[...,3] 等效于 x[:,:,:,:,3]
# x[4,...,5,:] 等效于 x[4,:,:,5,:]
# a 3D array (two stacked 2D arrays)
c = np.array( [[[ 0, 1, 2],
[ 10, 12, 13]],
[[100,101,102],
[110,112,113]]])
print("数组c的轴(维度)的个数:",c.ndim)
print("数组c的维度:",c.shape)
print("第一个轴的第二个元素:\n",c[1,...]) # same as c[1,:,:] or c[1]
print("最后一个轴的第三个元素:\n",c[...,2]) # same as c[:,:,2]
数组c的轴(维度)的个数: 3
数组c的维度: (2, 2, 3)
第一个轴的第二个元素:
[[100 101 102]
[110 112 113]]
最后一个轴的第三个元素:
[[ 2 13]
[102 113]]
形状操纵
改变数组的形状
#一个数组的形状是由每个轴的元素数量决定的
a = np.floor(10*np.random.random((3,4)))
print("a.shape:",a.shape)
print(a)
b=a.copy()
#扁平化操作
c = a.ravel()#c里面的值改变的话,a也会跟着改变
print(c) # returns the array, flattened
d = b.flatten()#d里面的值改变的话,不影响b
print(d)
c[1]=99
d[1]=99
print(a)
print(b)
a.shape: (3, 4)
[[8. 4. 4. 0.]
[7. 9. 6. 6.]
[4. 9. 1. 2.]]
[8. 4. 4. 0. 7. 9. 6. 6. 4. 9. 1. 2.]
[8. 4. 4. 0. 7. 9. 6. 6. 4. 9. 1. 2.]
[[ 8. 99. 4. 0.]
[ 7. 9. 6. 6.]
[ 4. 9. 1. 2.]]
[[8. 4. 4. 0.]
[7. 9. 6. 6.]
[4. 9. 1. 2.]]
#改变数组的形状
print(a.shape,"\n",a)
print( a.reshape(6,2))
print(a.shape,"\n",a)
print("a的转置:",a.T)
print(a.shape)
print(a.T.shape)
#而该 ndarray.resize 方法会修改数组本身
a.resize((2,6))
print(a.shape,"\n",a)
(3, 4)
[[ 7. 99. 2. 5.]
[ 3. 5. 5. 6.]
[ 2. 5. 5. 5.]]
[[ 7. 99.]
[ 2. 5.]
[ 3. 5.]
[ 5. 6.]
[ 2. 5.]
[ 5. 5.]]
(3, 4)
[[ 7. 99. 2. 5.]
[ 3. 5. 5. 6.]
[ 2. 5. 5. 5.]]
a的转置: [[ 7. 3. 2.]
[99. 5. 5.]
[ 2. 5. 5.]
[ 5. 6. 5.]]
(3, 4)
(4, 3)
(2, 6)
[[ 7. 99. 2. 5. 3. 5.]
[ 5. 6. 2. 5. 5. 5.]]