import numpy as np
a = np.array([[1,2,3],[4,3,2],[6,3,5]])
print(a)
[[1 2 3]
[4 3 2]
[6 3 5]]
print(a.ndim) # array的维度
2
print(a.dtype) # array元素的数据类型
int32
print(a.shape) # array每个维度的大小
(3, 3)
print(a.T) # array的转置
[[1 4 6]
[2 3 3]
[3 2 5]]
print(a.real) # array每个元素的实部
[[1 2 3]
[4 3 2]
[6 3 5]]
print(a.imag) # array每个元素的虚部
[[0 0 0]
[0 0 0]
[0 0 0]]
print(a.flat[1],a.flat[2:5]) # 将array变成一维的基础上对其进行索引
print(a)
a.flat[[1,4]]=8 # 将array对应位置的值进行改变
print(a)
2 [3 4 3]
[[1 2 3]
[4 3 2]
[6 3 5]]
[[1 8 3]
[4 8 2]
[6 3 5]]
b = a.tolist() # 将array变成python列表并返回
print(b)
[[1, 8, 3], [4, 8, 2], [6, 3, 5]]
c = a.copy() # 拷贝array并返回,对得到array进行改变不会影响原来的array
c[1,1] = 100
print("c:",c)
print("a:",a)
print(id(c),id(a))
c: [[ 1 8 3]
[ 4 100 2]
[ 6 3 5]]
a: [[1 8 3]
[4 8 2]
[6 3 5]]
1319291177280 1319291122112
d = a.view() # 将array的数据进行另外一个可视化,两者id不同,但进行修改时会影响
print(id(d),id(a))
d[1,1] = 200
print("d:",d)
print("a:",a)
a[1,1] = 150
print("d:",d)
print("a:",a)
1319291176960 1319291122112
d: [[ 1 8 3]
[ 4 200 2]
[ 6 3 5]]
a: [[ 1 8 3]
[ 4 200 2]
[ 6 3 5]]
d: [[ 1 8 3]
[ 4 150 2]
[ 6 3 5]]
a: [[ 1 8 3]
[ 4 150 2]
[ 6 3 5]]
a.fill(5) # 以指定的值填充array
print(a)
[[5 5 5]
[5 5 5]
[5 5 5]]
a = np.array([[1,3,5],[2,6,3],[9,6,4],[10,2,8]])
b = a.reshape((2,3,2)) # 在原数据的基础上改变维度,返回一个新array,不改变原来的array
print(a)
print(b)
[[ 1 3 5]
[ 2 6 3]
[ 9 6 4]
[10 2 8]]
[[[ 1 3]
[ 5 2]
[ 6 3]]
[[ 9 6]
[ 4 10]
[ 2 8]]]
c = b.transpose() # 返回一个转置后的array,可以指定维度顺序,不改变原来的array
print(c,"\n================")
d = b.transpose(0,2,1)
print(d)
[[[ 1 9]
[ 5 4]
[ 6 2]]
[[ 3 6]
[ 2 10]
[ 3 8]]]
================
[[[ 1 5 6]
[ 3 2 3]]
[[ 9 4 2]
[ 6 10 8]]]
print(b)
e = b.flatten() # 把array变成一维的
print(e)
f = b.ravel()
print(f)
[[[ 1 3]
[ 5 2]
[ 6 3]]
[[ 9 6]
[ 4 10]
[ 2 8]]]
[ 1 3 5 2 6 3 9 6 4 10 2 8]
[ 1 3 5 2 6 3 9 6 4 10 2 8]
g = b.take([0,6]) # 通过索引获取array中的元素并返回新的array,可以通过axis参数指定维度
h = b.take([0,2],axis=1)
i = b.take(1,axis=0)
print(g,"\n===================")
print(h,"\n===================")
print(i)
[1 9]
===================
[[[1 3]
[6 3]]
[[9 6]
[2 8]]]
===================
[[ 9 6]
[ 4 10]
[ 2 8]]
b = np.array([[[ 1,3],
[ 5 ,2],
[ 6 ,3]],
[[ 9 ,6],
[ 4 ,10],
[ 2 ,8]]])
print(b,"\n============================")
b.sort(axis=0)
print(b,"\n============================")
b.sort(axis=1)
print(b,"\n============================")
b = np.array([[[ 1,3],
[ 5 ,2],
[ 6 ,3]],
[[ 9 ,6],
[ 4 ,10],
[ 2 ,8]]])
b.sort()
print(b)
"""
sort进行排序时,可以通过axis指定维度,默认为最后一维,正序
当指定axis=0时,在第一个维度上排序,本例中该维度上有2个部分。
排序时,把两部分当做两个独立的array比较大小(元素位置一一对应),
顺序不符合要求的进行交换。当axis=1时,在第二个维度上排序,
此时不考虑外部第一个维度中两个部分的顺序,只分别对第二个维度上
三个小array进行同样的排序
"""
print(b.max(axis=-1)) # 返回最大值,最小值,不指定axis时,在所有元素中进行比较
print(b.max())
print(b.min())
[[ 3 5 6]
[ 9 10 8]]
10
1
print(b.argmax(axis=1)) # 返回最大值,最小值的索引,不指定axis时,在所有元素中进行比较
print(b.argmin())
[[2 2]
[0 1]]
0
print(b.ptp()) # 返回最大值,最小值的差值,不指定axis时,在所有元素中进行比较
print(b.ptp(axis=1))
9
[[2 3]
[4 2]]
c = b.clip([2,7]) # 将array中所有元素限制在指定的大小范围,超出范围的用指定的边界值代替
print(c)
[[[ 2 7]
[ 2 7]
[ 3 7]]
[[ 6 9]
[ 4 10]
[ 2 8]]]
a = np.arange(9).reshape(3,3) #求array对角线上元素的和
print(a.trace())
b = np.arange(8).reshape(2,4)
print(b.trace())
12
5
a = np.floor(10*np.random.random((2,3,4)))
print(a)
print(a.sum()) # 求和
print(a.sum(axis=1))
[[[0. 0. 1. 8.]
[0. 3. 0. 2.]
[5. 3. 9. 8.]]
[[9. 0. 1. 5.]
[7. 6. 9. 6.]
[7. 5. 6. 7.]]]
107.0
[[ 5. 6. 10. 18.]
[23. 11. 16. 18.]]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.cumsum()) # 求累加和
print(a.cumsum(axis=1))
[[7. 3. 1.]
[6. 8. 8.]]
[ 7. 10. 11. 17. 25. 33.]
[[ 7. 10. 11.]
[ 6. 14. 22.]]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.mean()) # 求均值
print(a.mean(axis=1))
[[6. 6. 1.]
[9. 8. 5.]]
5.833333333333333
[4.33333333 7.33333333]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.var()) # 求方差
print(a.var(axis=1))
[[7. 0. 0.]
[2. 0. 4.]]
6.805555555555557
[10.88888889 2.66666667]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.std()) # 求标准差
print(a.std(axis=0))
[[9. 1. 1.]
[6. 4. 1.]]
3.0368111930481
[1.5 1.5 0. ]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.prod()) # 求积
print(a.prod(axis=0))
[[5. 7. 4.]
[8. 9. 8.]]
80640.0
[40. 63. 32.]
a = np.floor(10*np.random.random((2,3)))
print(a)
print(a.cumprod()) # 求累积
print(a.cumprod(axis=1))
[[1. 1. 8.]
[9. 6. 6.]]
[1.000e+00 1.000e+00 8.000e+00 7.200e+01 4.320e+02 2.592e+03]
[[ 1. 1. 8.]
[ 9. 54. 324.]]
a = np.array([[1,0,2],[-1,3,5]])
print(a.all()) # 所有元素都为True,返回True,否则返回Flase
a = np.array([[1,2,2],[-1,3,5]])
print(a.all())
False
True
来源:oschina
链接:https://my.oschina.net/u/4335915/blog/4035772