numpy学习
In [8]:
import numpy as np
array = np.array([[1,2,3],
[2,3,5]])
print(array)
[[1 2 3]
[2 3 5]]
In [9]:
array.ndim
Out[9]:
2
In [10]:
array.shape
Out[10]:
(2, 3)
In [11]:
array.size
Out[11]:
6
In [17]:
a=np.array([1,2,3],dtype=np.int)
print(a)
print(a.dtype)
b=np.array([1,2,3],dtype=np.float)
print(b)
print(b.dtype)
[1 2 3]
int32
[ 1. 2. 3.]
float64
In [19]:
array0 = np.zeros((3,4))
print(array0)
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
In [21]:
array1 = np.ones((3,4))
print(array1)
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
In [23]:
array2 = np.empty((3,4))
print(array2)
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
In [24]:
array3 = np.arange(10,20,2)
print(array3)
[10 12 14 16 18]
In [26]:
array4 = np.arange(12).reshape((3,4))
print(array4)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
In [27]:
array5 = np.linspace(1,10,25)
print(array5)
[ 1. 1.375 1.75 2.125 2.5 2.875 3.25 3.625 4.
4.375 4.75 5.125 5.5 5.875 6.25 6.625 7. 7.375
7.75 8.125 8.5 8.875 9.25 9.625 10. ]
In [30]:
array6 = np.linspace(1,10,12).reshape((3,4))
print(array6)
[[ 1. 1.81818182 2.63636364 3.45454545]
[ 4.27272727 5.09090909 5.90909091 6.72727273]
[ 7.54545455 8.36363636 9.18181818 10. ]]
In [38]:
a=np.array([10,20,30,40])
b=np.arange(4)
c=a-b
print(c)
[10 19 28 37]
In [34]:
c=b**3
print(c)
[ 0 1 8 27]
In [35]:
c=10*np.sin(a)
print(c)
[-5.44021111 9.12945251 -9.88031624 7.4511316 ]
In [36]:
print(b)
print(b<3)
print(b==3)
[0 1 2 3]
[ True True True False]
[False False False True]
In [44]:
a=np.array([[1,2,3],[3,4,5]])
b=np.arange(6).reshape((3,2))
print(a)
print(b)
#c=a*b
c_dot=np.dot(a,b)
print(c_dot)
c_dot2=a.dot(b)
print(c_dot2)
[[1 2 3]
[3 4 5]]
[[0 1]
[2 3]
[4 5]]
[[16 22]
[28 40]]
[[16 22]
[28 40]]
In [50]:
a=np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.max(a))
print(np.min(a))
[[ 0.4601967 0.93594758 0.5499286 0.41483107]
[ 0.5729537 0.04874679 0.26190708 0.5702891 ]]
3.81480060629
0.935947580711
0.0487467894088
In [52]:
a=np.random.random((2,4))
print(a)
print(np.sum(a,axis=1)) #axis=1为行
print(np.max(a,axis=0)) #axis=0为列
print(np.min(a,axis=1)) #行
[[ 0.47054195 0.44146948 0.71298909 0.8230615 ]
[ 0.155426 0.06085024 0.36118835 0.45072419]]
[ 2.44806202 1.02818877]
[ 0.47054195 0.44146948 0.71298909 0.8230615 ]
[ 0.44146948 0.06085024]
In [67]:
A=np.arange(2,14).reshape((3,4))
print(A)
print(np.argmin(A))
print(np.argmax(A))
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
0
11
In [63]:
print(np.mean(A))
print(A.mean()) #平均值
print(np.average(A)) #平均值
print(np.median(A)) #中位数
7.5
7.5
7.5
7.5
In [68]:
print(np.cumsum(A)) #累加
[ 2 5 9 14 20 27 35 44 54 65 77 90]
In [69]:
print(np.diff(A)) #累差
[[1 1 1]
[1 1 1]
[1 1 1]]
In [72]:
print(np.nonzero(A)) #
(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))
In [74]:
A=np.arange(14,2,-1).reshape((3,4))
print(A)
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]]
In [78]:
print(A)
print(A.T) #行列数交换。矩阵反向 也可以表示成transpose(A)
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]]
[[332 302 272 242]
[302 275 248 221]
[272 248 224 200]
[242 221 200 179]]
In [79]:
print(A)
print(np.clip(A,5,9)) #小于5大于9的都替换成5或9,其他数保留不变。
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
[[9 9 9 9]
[9 9 8 7]
[6 5 5 5]]
In [81]:
print(A)
print(np.mean(A,axis=1)) #行平均值
print(np.mean(A,axis=0)) #列平均值
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
[ 12.5 8.5 4.5]
[ 10. 9. 8. 7.]
In [86]:
A=np.arange(3,15).reshape((3,4))
print(A)
print(A[2]) #同 A[2,:] 第3行的所有数
print(A[2][1]) #同A[2,1]
print(A[:,1]) #第一列的所有数
print(A[1,1:3])
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[11 12 13 14]
12
[ 4 8 12]
[8 9]
In [87]:
A=np.arange(3,15).reshape((3,4))
print(A)
for row in A:
print(row)
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
In [89]:
A=np.arange(3,15).reshape((3,4))
print(A)
for column in A.T:
print(column)
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
In [91]:
A=np.arange(3,15).reshape((3,4))
print(A)
print(A.flatten())
for i in A.flat:
print(i)
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[ 3 4 5 6 7 8 9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14
In [96]:
A=np.array([1,2,3])
B=np.array([4,5,6])
c=np.vstack((A,B)) #上下合并
print(A.shape)
print(c)
print(c.shape)
(3,)
[[1 2 3]
[4 5 6]]
(2, 3)
In [97]:
d=np.hstack((A,B)) #左右合并
print(d)
print(d.shape)
[1 2 3 4 5 6]
(6,)
In [99]:
print(A)
print(A.T)
[1 2 3]
[1 2 3]
In [105]:
print(A)
print(A[:,np.newaxis],A[:,np.newaxis].shape)
[1 2 3]
[[1]
[2]
[3]] (3, 1)
In [104]:
print(A,A.shape)
print(A[np.newaxis:],A[np.newaxis:].shape)
[1 2 3] (3,)
[1 2 3] (3,)
In [107]:
A=np.array([1,2,3])[:,np.newaxis] #以列作为维度
B=np.array([4,5,6])[:,np.newaxis]
c=np.vstack((A,B)) #上下合并
d=np.hstack((A,B)) #左右合并
print(A)
print(B)
print(c)
print(d)
[[1]
[2]
[3]]
[[4]
[5]
[6]]
[[1]
[2]
[3]
[4]
[5]
[6]]
[[1 4]
[2 5]
[3 6]]
In [112]:
A=np.array([1,2,3])[:,np.newaxis] #以列作为维度
B=np.array([4,5,6])[:,np.newaxis]
e=np.concatenate((A,B,B,A),axis=0) #按列合并。等同vstack((A,B)) 上下合并
print(e)
f=np.concatenate((A,B,B,A),axis=1) #按行合并。等同hstack((A,B)) 左右合并
print(f)
[[1]
[2]
[3]
[4]
[5]
[6]
[4]
[5]
[6]
[1]
[2]
[3]]
[[1 4 4 1]
[2 5 5 2]
[3 6 6 3]]
In [120]:
A=np.arange(12).reshape((3,4))
print(A)
b=np.split(A,2,axis=1) #axis=1 按列来分割
print(b)
c=np.split(A,3,axis=0) #axis=0 按行来分割
print(c)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
In [122]:
print(A)
d=np.array_split(A,3,axis=1) #不等分割
print(d)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
In [124]:
print(A)
b=np.vsplit(A,3) #上下分割 按行分割 同 split(A,3,axis=0)
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
In [125]:
print(A)
b=np.hsplit(A,2) #左右分割 按列分割 同 split(A,2,axis=1)
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
In [130]:
a=np.array([1,2,3,4])
b=a
c=a
d=b
print(a,b,c,d)
print(b is a)
a[0]=11
print(a,b,c,d)
b[1:3]=[22,33]
print(a,b,c,d)
[1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4]
True
[11 2 3 4] [11 2 3 4] [11 2 3 4] [11 2 3 4]
[11 22 33 4] [11 22 33 4] [11 22 33 4] [11 22 33 4]
In [137]:
a=np.array([1,2,3,4])
e=a.copy() #deep copy
print(e,a)
print(e is a)
a[0]=55
print(e,a)
[1 2 3 4] [1 2 3 4]
False
[1 2 3 4] [55 2 3 4]