numpy.array 参数类型要一样,而list参数可以不同 import numpy vector=numpy.array([5,6,7,8,9]) matrix=numpy.array([[1,2,3],[2,3,4],[4,5,6]]) print(vector) print(matrix) print(vector.shape) print(matrix.shape) vector.dtype >>> [5 6 7 8 9] [[1 2 3] [2 3 4] [4 5 6]] (5,) (3, 3) dtype('int32')
matrix=numpy.array([[1,2,3],[2,3,4],[4,5,6]]) vector=numpy.array([5,6,7,8,9]) print(vector[1:4]) print(matrix[:,:2]) vector==7 matrix==3 >>> [6 7 8] [[1 2] [2 3] [4 5]] Out[20]: array([[False, False, True], [False, True, False], [False, False, False]], dtype=bool)
vector=numpy.array([5,6,7,8,9]) equal_seven=(vector==7) print(vector[equal_seven]) >>> [7] vector=numpy.array([5,6,7,8,9]) equal_seven_and_eight=(vector==7)&(vector==8) print(equal_seven_and_eight) equal_seven_or_eight=(vector==7)|(vector==8) print(equal_seven_or_eight) >>> [False False False False False] [False False True True False]
vector=numpy.array([5,6,7,8,9]) print(vector.dtype) vectors=vector.astype(float) print(vectors.dtype) print(vectors) >>> int32 float64 [ 5. 6. 7. 8. 9.]
vector=numpy.array([5,6,7,8,9]) print(vector.min())#求最小值 >>> 5 matrix=numpy.array([[1,2,3],[2,3,4],[4,5,6]]) print(matrix.sum(axis=0))#按列求和 print(matrix.sum(axis=1))#按行求和 >>> [ 7 10 13] [ 6 9 15]
import numpy as np print(np.arange(15))#生成数列 a=np.arange(15).reshape(3,5)#将数列变成矩阵 print(a) >>> [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] a.shape#行列数 >>> (3, 5) a.ndim #矩阵的维度 >>> 2 a.dtype.name#类型 >>> 'int32' a.size#总共元素的个数 >>> 15
np.zeros((3,4))#创建0矩阵,它默认float类型 >>> array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) np.ones((2,3,4),dtype=np.int32)#2-矩阵个数,3-矩阵行数,4-矩阵列数,将类型转化为int型 >>> array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]])
np.arange(10,30,5)#以10开始,0结束,步长为5生成向量 >>> array([10, 15, 20, 25])
np.random.random((2,3))#-1到+1之间的数,组成2行3列矩阵 >>> array([[ 0.34030141, 0.07582737, 0.09042225], [ 0.43623825, 0.28065344, 0.40964751]]) from numpy import pi#引入pi np.linspace( 0, 2*pi, 100 )#在0到2*pi之间平均生成20个数 >>> array([ 0. , 0.33069396, 0.66138793, 0.99208189, 1.32277585, 1.65346982, 1.98416378, 2.31485774, 2.64555171, 2.97624567, 3.30693964, 3.6376336 , 3.96832756, 4.29902153, 4.62971549, 4.96040945, 5.29110342, 5.62179738, 5.95249134, 6.28318531]) In [ ]:
a=np.array([10,11,12,13]) b=np.arange(4) print(a) print(b) c=a-b print(c) c=c-1 print(c) print(b**2) print(a<3) >>> [10 11 12 13] [0 1 2 3] [10 10 10 10] [9 9 9 9] [0 1 4 9] [False False False False]
A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] ) print (A) print ('-------') print (B) print ('-------') print (A*B)#求内积 print ('-------') print (A.dot(B))#矩阵a与矩阵b相乘方法1 print ('-------') print (np.dot(A, B)) #矩阵a与矩阵b相乘方法2 >>> [[1 1] [0 1]] ------- [[2 0] [3 4]] ------- [[2 0] [0 4]] ------- [[5 4] [3 4]] ------- [[5 4] [3 4]]
import numpy as np B=np.arange(3) print(B) print(np.exp(B))#e的1次,e的2次,e的3次 print(np.sqrt(B))#0,1,2开根号 >>> [0 1 2] [ 1. 2.71828183 7.3890561 ] [ 0. 1. 1.41421356]
a = np.floor(10*np.random.random((3,4)))#floor取整 print ('a',a) print ('--------') b=a.ravel()#将矩阵变成向量 print('b',b) print ('--------') c=b.reshape(6,2)#将向量变成矩阵 print('c',c) print ('--------') d=c.shape = (2, 6) print ('d',d) print ('--------') print (a.T)#a转置 >>> a [[ 5. 2. 8. 7.] [ 0. 7. 9. 2.] [ 0. 8. 0. 0.]] -------- b [ 5. 2. 8. 7. 0. 7. 9. 2. 0. 8. 0. 0.] -------- c [[ 5. 2.] [ 8. 7.] [ 0. 7.] [ 9. 2.] [ 0. 8.] [ 0. 0.]] -------- d (2, 6) -------- [[ 5. 0. 0.] [ 2. 7. 8.] [ 8. 9. 0.] [ 7. 2. 0.]]
import numpy as np a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print (a) print ('---') print (b) print ('---') print (np.vstack((a,b)))#将两个矩阵拼接 #np.hstack((a,b)) >>> [[ 3. 7.] [ 2. 6.]] --- [[ 9. 6.] [ 0. 7.]] --- [[ 3. 7.] [ 2. 6.] [ 9. 6.] [ 0. 7.]]
import numpy as np a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print (a) print ('---') print (b) print ('---') print (np.hstack((a,b)))#将两个矩阵按行拼接 >>> [[ 4. 1.] [ 5. 3.]] --- [[ 6. 3.] [ 4. 4.]] --- [[ 4. 1. 6. 3.] [ 5. 3. 4. 4.]] In [9]:
a = np.floor(10*np.random.random((2,12))) print (a) print ('---') print (np.hsplit(a,3))#将矩阵a按行平均切成3份 print ('---') print (np.hsplit(a,(3,4))) #将矩阵a按行,在3处切一次,在4处切一次 a = np.floor(10*np.random.random((12,2))) print ('---') print (a) np.vsplit(a,3)#将矩阵a按列平均切成3份 >>> [[ 9. 8. 3. 2. 5. 6. 4. 7. 2. 5. 6. 7.] [ 3. 8. 3. 5. 2. 6. 6. 0. 1. 8. 9. 1.]] --- [array([[ 9., 8., 3., 2.], [ 3., 8., 3., 5.]]), array([[ 5., 6., 4., 7.], [ 2., 6., 6., 0.]]), array([[ 2., 5., 6., 7.], [ 1., 8., 9., 1.]])] --- [array([[ 9., 8., 3.], [ 3., 8., 3.]]), array([[ 2.],[ 5.]]), array([[ 5., 6., 4., 7., 2., 5., 6., 7.], [ 2., 6., 6., 0., 1., 8., 9., 1.]])] --- [[ 4. 4.] [ 3. 5.] [ 9. 7.] [ 6. 0.] [ 5. 8.] [ 1. 4.] [ 1. 5.] [ 7. 1.] [ 2. 5.] [ 0. 4.] [ 1. 0.] [ 3. 1.]] Out[3]: [array([[ 4., 4.], [ 3., 5.], [ 9., 7.], [ 6., 0.]]), array([[ 5., 8.], [ 1., 4.], [ 1., 5.], [ 7., 1.]]), array([[ 2., 5.], [ 0., 4.], [ 1., 0.], [ 3., 1.]])]