numpy学习
numpy学习
In [8]:
import numpy as np
array = np.array([[1,2,3],
[2,3,5]])
print(array)
In [9]:
array.ndim
Out[9]:
In [10]:
array.shape
Out[10]:
In [11]:
array.size
Out[11]:
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)
In [19]:
array0 = np.zeros((3,4))
print(array0)
In [21]:
array1 = np.ones((3,4))
print(array1)
In [23]:
array2 = np.empty((3,4))
print(array2)
In [24]:
array3 = np.arange(10,20,2)
print(array3)
In [26]:
array4 = np.arange(12).reshape((3,4))
print(array4)
In [27]:
array5 = np.linspace(1,10,25)
print(array5)
In [30]:
array6 = np.linspace(1,10,12).reshape((3,4))
print(array6)
In [38]:
a=np.array([10,20,30,40])
b=np.arange(4)
c=a-b
print(c)
In [34]:
c=b**3
print(c)
In [35]:
c=10*np.sin(a)
print(c)
In [36]:
print(b)
print(b<3)
print(b==3)
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)
In [50]:
a=np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.max(a))
print(np.min(a))
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)) #行
In [67]:
A=np.arange(2,14).reshape((3,4))
print(A)
print(np.argmin(A))
print(np.argmax(A))
In [63]:
print(np.mean(A))
print(A.mean()) #平均值
print(np.average(A)) #平均值
print(np.median(A)) #中位数
In [68]:
print(np.cumsum(A)) #累加
In [69]:
print(np.diff(A)) #累差
In [72]:
print(np.nonzero(A)) #
In [74]:
A=np.arange(14,2,-1).reshape((3,4))
print(A)
print(np.sort(A)) #按行排序
In [78]:
print(A)
print(A.T) #行列数交换。矩阵反向 也可以表示成transpose(A)
print(A.T.dot(A)) #求矩阵相乘
In [79]:
print(A)
print(np.clip(A,5,9)) #小于5大于9的都替换成5或9,其他数保留不变。
In [81]:
print(A)
print(np.mean(A,axis=1)) #行平均值
print(np.mean(A,axis=0)) #列平均值
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])
In [87]:
A=np.arange(3,15).reshape((3,4))
print(A)
for row in A:
print(row)
In [89]:
A=np.arange(3,15).reshape((3,4))
print(A)
for column in A.T:
print(column)
In [91]:
A=np.arange(3,15).reshape((3,4))
print(A)
print(A.flatten())
for i in A.flat:
print(i)
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)
In [97]:
d=np.hstack((A,B)) #左右合并
print(d)
print(d.shape)
In [99]:
print(A)
print(A.T)
In [105]:
print(A)
print(A[:,np.newaxis],A[:,np.newaxis].shape)
In [104]:
print(A,A.shape)
print(A[np.newaxis:],A[np.newaxis:].shape)
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)
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)
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)
In [122]:
print(A)
d=np.array_split(A,3,axis=1) #不等分割
print(d)
In [124]:
print(A)
b=np.vsplit(A,3) #上下分割 按行分割 同 split(A,3,axis=0)
print(b)
In [125]:
print(A)
b=np.hsplit(A,2) #左右分割 按列分割 同 split(A,2,axis=1)
print(b)
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)
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)