《Python数据分析基础教程:Numpy学习指南》- 速记 - 第四章

4.2 股票相关性分析

covariance = np.cov(a,b)

详解协方差与协方差矩阵

获取对角元素
covariance.diagonal()

4.4 多项式拟合

bhp=np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)
vale=np.loadtxt('VALE.csv', delimiter=',', usecols=(6,),unpack=True)
t = np.arange(len(bhp))
poly = np.polyfit(t, bhp - vale, int(sys.argv[1])) # sys.argv[1]为3,即用3阶多项式拟合数据
print "Polynomial fit", poly

#output
Polynomial fit [ 1.11655581e-03 -5.28581762e-02 5.80684638e-01 5.79791202e+01]
#预测下个值
print "Next value", np.polyval(poly, t[-1] + 1)

使用polyder函数对多项式函数求导(以求极值)

der = np.polyder(poly)
print "Derivative", der
#output
Derivative [ 0.00334967 -0.10571635 0.58068464]

求出导数函数的根,即找出原多项式函数的极值点

print "Extremas", np.roots(der)
#output
Extremas [ 24.47820054 7.08205278]

注:书中提示,3阶多项式拟合数据的结果并不好,可尝试更高阶的多项式拟合。

4.6 计算OBV(On-Balance Volume)净额成交量

diff函数可以计算数组中两个连续元素的差值,并返回一个由这些差值组成的数组。
change = np.diff(c)

sign函数可以返回数组中每个元素的正负符号,数组元素为负时返回-1,为正时返回1,否则返回0
np.sign(change)

使用piecewise(分段的)函数来获取数组元素的正负。使用合适的返回值和对应的条件调用该函数:

pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1])
print "Pieces", pieces

检查一致性
np.array_equal(a, b)

np.vectorize 替代循环

>>> def myfunc(a, b):
...     "Return a-b if a>b, otherwise return a+b"
...     if a > b:
...         return a - b
...     else:
...         return a + b

>>> vfunc = np.vectorize(myfunc)
>>> vfunc([1, 2, 3, 4], 2)
array([3, 4, 1, 2])

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

4.10 使用hanning 函数平滑数据

(1) 调用hanning函数计算权重,生成一个长度为N的窗口(在这个示例中N取8)

N = int(sys.argv[1])
weights = np.hanning(N)
print "Weights", weights

#output
Weights [ 0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ]

bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,),unpack=True)   #某股票数据
bhp_returns = np.diff(bhp) / bhp[ : -1] #股票收益率计算
smooth_bhp = np.convolve(weights/weights.sum(), bhp_returns) [N-1:-N+1]  #使用weights平滑股票收益率

#绘图
t = np.arange(N - 1, len(bhp_returns))
plot(t, bhp_returns[N-1:], lw=1.0)
plot(t, smooth_bhp, lw=2.0)
show()

两个多项式做差运算
poly_sub = np.polysub(a, b)

选择函数
numpy.select(condlist, choicelist, default=0)

>>> x = np.arange(10)
>>> condlist = [x<3, x>5]  
#输出两个array [true...false...],[false,...true]
>>> choicelist = [x, x**2]
>>> np.select(condlist, choicelist)  
array([ 0,  1,  2,  0,  0,  0, 36, 49, 64, 81])

trim_zeros函数可以去掉一维数组中开头和末尾为0的元素:
np.trim_zeros(a)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值