大家好,Bulbea 是一个基于深度学习开发的,用于股票市场预测和建模的Python库。Bulbea 自带了不少可用于股票深度学习训练及测试的API,并且易于对数据进行扩展和延申,构建属于我们自己的数据及模型。
Bulbea 和普通的深度学习研究项目一样,在做训练和测试时,分为四步(加载数据,预处理,建模,测试)。
Bulbea内置了数据下载模块,让你很轻易地能够下载雅虎财经的股票数据,比如下面下载雅虎财经源的GOOGL股票数据:
>>> import bulbea as bb
>>> share = bb.Share('YAHOO', 'GOOGL')
>>> share.data
# Open High Low Close Volume \
# Date
# 2004-08-19 99.999999 104.059999 95.959998 100.339998 44659000.0
# 2004-08-20 101.010005 109.079998 100.500002 108.310002 22834300.0
# 2004-08-23 110.750003 113.479998 109.049999 109.399998 18256100.0
# 2004-08-24 111.239999 111.599998 103.570003 104.870002 15247300.0
# 2004-08-25 104.960000 108.000002 103.880003 106.000005 9188600.0
...
Bulbea 同样也内置了预处理模块,让你能够轻易地分割训练集和测试集:
>>> from bulbea.learn.evaluation import split
>>> Xtrain, Xtest, ytrain, ytest = split(share, 'Close', normalize = True)
Bulbea自带了RNN模型可供使用:
>>> import numpy as np
>>> Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], Xtrain.shape[1], 1))
>>> Xtest = np.reshape( Xtest, ( Xtest.shape[0], Xtest.shape[1], 1))
>>> from bulbea.learn.models import RNN
>>> rnn = RNN([1, 100, 100, 1]) # number of neurons in each layer
>>> rnn.fit(Xtrain, ytrain)
# Epoch 1/10
# 1877/1877 [==============================] - 6s - loss: 0.0039
# Epoch 2/10
# 1877/1877 [==============================] - 6s - loss: 0.0019
...
通过调用sklearn的metrics就能对数据实现测试:
>>> from sklearn.metrics import mean_squared_error
>>> p = rnn.predict(Xtest)
>>> mean_squared_error(ytest, p)
0.00042927869370525931
>>> import matplotlib.pyplot as pplt
>>> pplt.plot(ytest)
>>> pplt.plot(p)
>>> pplt.show()
Bulbea 能自动爬取相关股票在推特上的文字,并对这些文字做一个情感分析。
只需要给Bulbea提供以下环境变量就能够进行感情色彩分析:
export BULBEA_TWITTER_API_KEY=""
export BULBEA_TWITTER_API_SECRET=""
export BULBEA_TWITTER_ACCESS_TOKEN=""
export BULBEA_TWITTER_ACCESS_TOKEN_SECRET=""
测试一下:
>>> import bulbea as bb
>>> share = bb.Share('YAHOO', 'GOOGL')
>>> bb.sentiment(share)
0.07580128205128206
由于较为粗略,这个分析仅供参考,如果喜欢今天的量化投资内容,请持续关注。