资源简介
利用深度学习的长短记忆原理(LSTM)对美国纳斯达克股市进行预测。
代码片段和文件信息
import time
import warnings
import numpy as np
from numpy import newaxis
from keras.layers.core import Dense Activation Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import matplotlib.pyplot as plt
warnings.filterwarnings(“ignore“)
def plot_results_multiple(predicted_data true_data prediction_len):
fig = plt.figure(facecolor=‘white‘)
ax = fig.add_subplot(111)
ax.plot(true_data label=‘True Data‘)
print ‘yo‘
#Pad the list of predictions to shift it in the graph to it‘s correct start
for i data in enumerate(predicted_data):
padding = [None for p in xrange(i * prediction_len)]
plt.plot(padding + data label=‘Prediction‘)
plt.legend()
plt.show()
def load_data(filename seq_len normalise_window):
f = open(filename ‘r‘).read()
data = f.split(‘\n‘)
sequence_length = seq_len + 1
result = []
for index in range(len(data) - sequence_length):
result.append(data[index: index + sequence_length])
if normalise_window:
result = normalise_windows(result)
result = np.array(result)
row = round(0.9 * result.shape[0])
train = result[:int(row) :]
np.random.shuffle(train)
x_train = train[: :-1]
y_train = train[: -1]
x_test = result[int(row): :-1]
y_test = result[int(row): -1]
x_train = np.reshape(x_train (x_train.shape[0] x_train.shape[1] 1))
x_test = np.reshape(x_test (x_test.shape[0] x_test.shape[1] 1))
return [x_train y_train x_test y_test]
def normalise_windows(window_data):
normalised_data = []
for window in window_data:
normalised_window = [((float(p) / float(window[0])) - 1) for p in window]
normalised_data.append(normalised_window)
return normalised_data
def build_model(layers):
model = Sequential()
model.add(LSTM(
input_dim=layers[0]
output_dim=layers[1]
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
layers[2]
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[3]))
model.add(Activation(“linear“))
start = time.time()
model.compile(loss=“mse“ optimizer=“rmsprop“)
print “Compilation Time : “ time.time() - start
return model
def predict_point_by_point(model data):
#Predict each timestep given the last sequence of true data in effect only predicting 1 step ahead each time
predicted = model.predict(data)
predicted = np.reshape(predicted (predicted.size))
return predicted
def predict_sequence_full(model data window_size):
#Shift the window by 1 new prediction each time re-run predictions on new window
curr_frame = data[0]
predicted = []
for i in xrange(len(data)):
predicted.append(model.predict(curr_frame[newaxis::])[00])
curr_frame = curr_frame[1:]
curr_frame = np.insert(curr_frame [window_size-1] predicted[-1] a
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-03-02 02:39 How-to-Predict-Stock-Prices-Easily-Demo-master\
文件 1398 2017-03-02 02:39 How-to-Predict-Stock-Prices-Easily-Demo-master\README.md
文件 3637 2017-03-02 02:39 How-to-Predict-Stock-Prices-Easily-Demo-master\lstm.py
文件 48119 2017-03-02 02:39 How-to-Predict-Stock-Prices-Easily-Demo-master\sp500.csv
文件 62673 2017-03-02 02:39 How-to-Predict-Stock-Prices-Easily-Demo-master\stockdemo.ipynb
相关资源
- 基于数据驱动的故障预测模型
- 提高风出力预测精度的储能系统模糊
- 电网负荷预测论文集9篇
- 基于蚁群算法优化SVM的瓦斯涌出量预
- 基于模糊聚类和SVM的瓦斯涌出量预测
- 最小预测跷跷板模型中的脂肪生成
- 中微子混合矩阵和无中微子双β衰
- 基于时序SAR技术的采空区上方高速公
- 约化分子电距矢量用于茎用莴苣花挥
- 基于CAN总线与ZigBee的瓦斯实时监测及
- 基于熵权法的PHC管桩承载力组合预测
- 基于动态分类器集成选择和GM(21)
- 缓冲算子理论在矿井瓦斯涌出量预测
- 瓦斯涌出量预测的GM(11)模型改进
- 矿井瓦斯涌出量建模预测
- 基于灰色系统理论的矿井瓦斯涌出量
- 矿井回采工作面瓦斯涌出量预测新途
- 矿井瓦斯涌出量的GM(11)新陈代谢模
- GM(11)模型与线性回归组合方法在矿
- 灰色系统理论在矿井瓦斯涌出量预测
- 等维新息模型在矿井瓦斯涌出量预测
- 基于改进灰色模型的矿井瓦斯涌出量
- GM(11)与ARIMA模型在中国一次能源消
- 基于时间加权-新陈代谢GM(11)模型在
- 基于新陈代谢GM(11)-ARMA(pq)模型的
- 改进的GM(11)模型在城市规划货运量
- 大型强子对撞机中单个矢量样夸克生
- 超外围碰撞中的排他和/ψ光产生:
- 《综合数据AI方法预测2019-nCoV传播结束
- 有源滤波器的预测电压空间矢量控制
评论
共有 条评论