《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 dtype
dataf = np.arange(7,dtype='f')
print dataf
#test the numpy double
datad = np.arange(7,dtype='D')
print datad
#test the numpy dtype
print np.dtype('f8')
#test the create the dataset
data_creat = np.dtype([('name',str,40),('num',int),('price',float)])
print data_creat
print data_creat['name']
#create a data for the new datatype
itemz = np.array([('Meaning of life DVD',42,3.14),('Butter',13,2.72)],dtype = data_creat)
print itemz
print 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 array
print a[::-1]
#test the reshape
#the function is resever() reshape() flatten() resize()
b = np.arange(24).reshape(2,3,4)
print b
print b.ravel()
#test the hstack()
a = np.arange(9).reshape(3,3)
b = 2*a
print a
print b
print 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 spilt
a = 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*2
print np.dsplit(np.vstack((a,b)),3)
#.T 效果为转置
a = np.arange(24).reshape(6,4)
print "转置前"
print a
print "转置后"
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)