Numpy 初试

来源:互联网 发布:阿里云华东1和华东2 编辑:程序博客网 时间:2024/06/05 08:16

《Numpy Beginner’s Guide》是数据挖掘中继《数据挖掘导论》之后又一本非常棒的书,尽管才开始学习它,但是它的强大让我深深的爱上了它的cool。
首先是最基本的安装问题,由于我是ubuntu er,So安装的过程比较简单,一行命令就全部搞定。
下面的代码纯属是为了方便自己以后在处理的时候,能很快找这个简单的函数,而不用又去翻书或者google。

#-*-encoding:utf-8-*-import numpy as np#test the numpy.arange datai =np.arange(10)print datai#test the numpy dtypedataf = np.arange(7,dtype='f')print dataf#test the numpy doubledatad = np.arange(7,dtype='D')print datad#test the numpy dtypeprint np.dtype('f8')#test the create the datasetdata_creat = np.dtype([('name',str,40),('num',int),('price',float)])print data_creat print data_creat['name']#create a data for the new datatypeitemz = np.array([('Meaning of life DVD',42,3.14),('Butter',13,2.72)],dtype = data_creat)print itemzprint itemz[1]print itemz[1][0]#test the data index#select the 3 to 7 not include 7 #so is [3,4,5,6]a = np.arange(9)print a[3:7]print a[:7:2]#resever the arrayprint a[::-1]#test the reshape #the function is resever()  reshape()  flatten()  resize()b = np.arange(24).reshape(2,3,4)print bprint b.ravel()#test the hstack()a = np.arange(9).reshape(3,3)b = 2*aprint aprint bprint np.hstack((a,b))#test the np.concatenate()  水平组合 np.concatenate()和hstack()函数效果相同  print np.concatenate((a,b),axis = 1)#test the vstack()  垂直组合   和 np.concatenate()函数效果相同 第二个参数不同print np.vstack((a,b))print np.concatenate((a,b),axis=0)#test the dstack()  深度组合 print np.dstack((a,b))#test the column_stack 列组合 二维效果和hstack效果一样column =  np.column_stack((np.array([1,2]),np.array([2,4])))print column#test the row_stack 行组合   二维效果和vstack效果一样row =  np.row_stack((np.array([1,2]),np.array([2,4])))print row#test the print row == column#test the spilta = np.arange(9).reshape(3,3)#水平分割 二者效果一样print np.hsplit(a,3)print np.split(a,3,axis=1)#垂直分割print np.vsplit(a,3)print np.split(a,3,axis=0)#深度分割a = np.arange(27).reshape(3,3,3)b = a*2print np.dsplit(np.vstack((a,b)),3)#.T 效果为转置a = np.arange(24).reshape(6,4)print "转置前"print aprint "转置后"print a.T#矩阵的属性#itemsize 给出数组元素在内存所占用的总个数#size 给出数组元素总个数#nbytes #T 转置#flat 扁平迭代器#ndim 数组维数#real 实部#imag 虚部#tolist函数将numpy数组转为列表print a.tolist()

第二章学习记录

#-*-encoding:utf-8-*-#itemsize#itemsize#读写文件import numpy as np#i = np.eye(2)#print i#存储到文件中#np.savetxt("eye.txt",i)c,v = np.loadtxt("data_test.csv",delimiter=",",usecols=(6,7),unpack = True)#量加权vwap = np.average(c,weights=v)print 'VWAP',vwap#计算平均值print np.mean(c)#时间加权time = np.arange(len(c))res = np.average(c,weights=time)print "TWAP",res#最大值  最小值print "最大值:",np.max(c)print "最小值:",np.min(c)print np.max(c)-np.min(c)#中位数print "中位数",np.median(c)#值区间print "Spread in the price",np.ptp(c)#排序print "Sort:",np.msort(v)#方差print "Variance:",np.var(c)#标准差print "Std:",np.std(c)
0 0
原创粉丝点击