1. Root and Seek E index
Import NumPy as Npfrom numpy.matlib import Randnprint "Test sqrt and exp" arr = Np.arange (Ten) print np.sqrt (arr) #开方print NP. Exp (arr) #求exp
2. Conditional merge
Print "Test Max-merge" #取x和y中对应位置较大的item组成新数组x =randn (8) Y=RANDN (8) Print X;print y;print np.maximum (x, y) print "test Condition-merge "#根据condition matrix,true take tmat,false take Fmattmat = Np.arange (fMat) Np.arange = 0,-10,-1 Conmat = [True] , False,true,false,true,false,true,false,true,false] Result=np.where (conmat,tmat,fmat) #condition Clauseprint Resultrdmmat = Randn (Ten) Result=np.where (Rdmmat>0,tmat,fmat) #the three matrix should has same sizeprint result
3, statistic functions
Print "Test statistics functions" RDMMAT1 = RANDN (10,3) print Rdmmat1.mean () #计算所有元素的均值print Np.mean (RDMMAT1) print Np.mean (RDMMAT1, 0) #column meanprint np.mean (RDMMAT1, 1) #row meanprint np.sum (RDMMAT1) print np.sum (rdmmat1,0) #column s Umprint np.sum (rdmmat1,1) #row sum
4. Boolean function
print "test bool functions" RDMMAT2 = RANDN (Ten) Numberoftrue = (rdmmat2>0). SUM () #true的个数print NUMBEROFTRUERDMMAT3 = NP. Array ([true,true,false,true]) print rdmmat3.any () #是否含有至少一个trueprint rdmmat3.all () #是否全为true
5. Sorting function
Print "Test Sort" print Np.sort (RDMMAT2) #np. Sort () doesn ' t change the original Matrixprint rdmmat2rdmmat2.sort () #python ' s sort changes the matrixprint rdmmat2rdm53 = Randn (5,3) print np.sort (rdm53,1) print rdm53rdm53.sort (1) Print Rdm53rdm53.sort (0) Print rdm53
6. Include Operation
Print "Test set operation" names = Np.array ([' Tom ', ' Dean ', ' Won ', ' Tom ', ' Tom ', ' Dean ']) print np.unique (names) #取出唯一的项, Arranging print Np.unique (names,1,1,1) #第二个参数 in dictionary order gives the index # of the first occurrence of these values # The third parameter, which gives the number of index positions in which these values occur # The fourth parameter, giving a count of the occurrences of these values
Python Numpy Matrix-level basic operations (2)