Python Numpy 矩阵级基本操作(2)
1、开方与求e指数
import numpy as np from numpy.matlib import randn print "Test sqrt and exp" arr = np.arange(10) print np.sqrt(arr)#开方 print np.exp(arr)#求exp
2、条件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时取tMat,false取fMat tMat = np.arange(10) fMat = np.arange(0,-10,-1) conMat = [True,False,True,False,True,False,True,False,True,False] result=np.where(conMat,tMat,fMat) #condition clause print result rdmMat = randn(10) result=np.where(rdmMat>0,tMat,fMat) #the three matrix should have same size print 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 mean print np.mean(rdmMat1, 1) #row mean print np.sum(rdmMat1) print np.sum(rdmMat1,0) #column sum print np.sum(rdmMat1,1) #row sum
4、布尔函数
print "test bool functions" rdmMat2 = randn(10) numberOfTrue = (rdmMat2>0).sum() #true的个数 print numberOfTrue rdmMat3 = np.array([True,True,False,True]) print rdmMat3.any() #是否含有至少一个true print rdmMat3.all() #是否全为true
5、排序函数
print "test sort" print np.sort(rdmMat2) #np.sort() doesn't change the original matrix print rdmMat2 rdmMat2.sort() #python's sort changes the matrix print rdmMat2 rdm53 = randn(5,3) print np.sort(rdm53,1) print rdm53 rdm53.sort(1) print rdm53 rdm53.sort(0) print rdm53
6、包含操作
print "test set operation" names = np.array(['Tom','Dean','Won','Tom','Tom','Dean']) print np.unique(names)#取出唯一的项,按照字典序排列 print np.unique(names,1,1,1) #第二个参数,会给出这几个值第一次出现的索引 #第三个参数,会给出这几个值出现的所有索引位置 #第四个参数,给出这几个值出现的次数
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16