ImportNumPy as NPA=np.array ([A])PrintAB=np.array ([[1,2],[3,4]])PrintbPrintNp.arange (1,5,0.5)#less than 5PrintNp.random.random ((2,2))#2*2 Random floating-point arrayPrintNp.linspace (1,2,10,endpoint=False)PrintNp.linspace (1,2,10)#The default 2 can fetchPrintNp.fromfunction (LambdaI,j: (i+1) * (j+1), [2,3]) C=np.array ([4,5,6])PrintNp.hstack ((a,c)) #横向连接PrintNp.vstack ((a,c)) #纵向连接Printb.t# TransposePrintA+c#add the corresponding elements
Ans
[1 2 3]
[[1 2]
[3 4]]
[1.1.5 2. 2.5 3. 3.5 4. 4.5]
[[0.54937261 0.22419401]
[0.41047764 0.53122061]]
[1.1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
[1.1.11111111 1.22222222 1.33333333 1.44444444 1.55555556
1.66666667 1.77777778 1.88888889 2. ]
[1.2. 6.8
[2.4. 6.]
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
[[1 3]
[2 4]]
To generate an array from list:
Import NumPy as NPA=np.array ([]) x=[1,2,3]xx=np.array (x)print XXY =[[1,2],[3,4]]yy=np.array (y)print yyprint yy.flatten () # Flatten , not action on list ans:[1 2 3][[1 2] [3 4]][1 2 3 4]
B=np.array ([[[1,2],[3,4]])print b.sum ()print b.sum (axis=0)# sum by column Print b.sum (axis=1)# sum by line print b.min ()print B.argmax ()#Max's subscript print B.mean ()
Print b.std ()b[1]out[117]: Array ([3, 4]) b[1][1]out[118]: 4
Ans
10
[4 6]
[3 7]
1
3
2.5
1.11803398875
Remark: The property function of an array does not need to be added at the end ()
B=np.array ([[[1,2],[3,4]])print b.size# number of elements print b.ndim# Rank Print B.shape print b.dtype# element type ans:2L, 2Lint32
NumPy is faster than math:
ImportNumPy as NPImport TimeImportMathx=np.arange (0,100,0.01) T1=Time.clock () forI,tinchEnumerate (x): X[i]=math.pow ((Math.sin (t)), 2) T2=time.clock () y=np.arange (0,100,0.01) T3=time.clock () y=np.power (Np.sin (y), 2) T4=Time.clock ()Print('running time of math is:', t2-T1)Print('running time of NumPy is:', t4-T3) ans: ('running time of math is:', 0.011655904997766612)('running time of NumPy is:', 0.0023625201675097404)
NumPy in combination with scipy:
ImportNumPy as NP fromSciPyImportLinalga=np.array ([[1,0],[1,2]])PrintNp.dot (a,a)PrintLinalg.det (a)PrintLINALG.INV (a) x, y=Linalg.eig (a)PrintX#characteristic valuePrintY#feature vectors?? An odd value, a characteristic vector given randomly.ans:[[10] [3 4]]2.0[[ 1.-0.] [-0.5 0.5]][ 2.+0.J 1.+0.J] [0.0.70710678] [ 1.-0.70710678]]
ImportNumPy as NP fromScipy.cluster.vqImportVq,kmeans,whitenlist1=[89,90,76,90]list2=[96,78,89,79]list3=[90,98,89,80]list4=[80,72,79,84]list5=[92,81,89,87]data=Np.array ([list1,list2,list3,list4,list5])PrintDatawhiten=whiten (data) centroids,_=kmeans (whiten,2) Result,_=VQ (whiten,centroids)Print(Result) ans:[[89 90 76 90] [96 78 89 79] [90 98 89 80] [80 72 79 84] [92 81 89 87]][1 0 0) 1 0]
Three-dimensional arrays:
ImportNumPy as NPB=np.arange (reshape) (2,3,4)PrintbPrintB[1]#Access second tierPrintB[0,1,::2]#from the beginning, the interval is 2.Printb[0,1,[1,2]]PrintB[::-1]#two-layer switchingPrintB.ravel ()#an array of flattening and changing one-dimensionalPrintB.reshape (6,4) x=np.array ([[1,2,3],[4,5,6],[7,8,9]])#Split ArrayX1,x2,x3=np.hsplit (x,3) PrintX1#get [1,4,7]Y1,y2,y3=np.vsplit (x,3)PrintY1#get [A]Z=np.array ([1+2.j,3+4. J])PrintZ.realPrintz.tolist ()Printx.tolist () ans:[[[01 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]][[12 13 14 15] [16 17 18 19] [20 21 22 23]][4 6][5 6][[[12 13 14 15] [16 17 18 19] [20 21 22 23]] [[01 2 3] [ 4 5 6 7] [ 8 9 10 11]]][01 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23][[01 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19] [20 21 22 23]][[1] [4] [7]][[1 2 3]][ 1.3.] [(1+2J), (3+4j)][[1, 2, 3], [4, 5, 6], [7, 8, 9]
Python (5): numpy-1:fundamental knowledge