资源简介
Time series forecasting using LSTM.
代码片段和文件信息
from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import Dataframe
from pandas import concat
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
# convert series to supervised learning
def series_to_supervised(data n_in=1 n_out=1 dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = Dataframe(data)
cols names = list() list()
# input sequence (t-n ... t-1)
for i in range(n_in 0 -1):
cols.append(df.shift(i))
names += [(‘var%d(t-%d)‘ % (j + 1 i)) for j in range(n_vars)]
# forecast sequence (t t+1 ... t+n)
for i in range(0 n_out):
cols.append(df.shift(-i))
if i == 0:
names += [(‘var%d(t)‘ % (j + 1)) for j in range(n_vars)]
else:
names += [(‘var%d(t+%d)‘ % (j + 1 i)) for j in range(n_vars)]
# put it all together
agg = concat(cols axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
def plot_results(predicted_data true_data):
fig = pyplot.figure(facecolor=‘white‘)
ax = fig.add_subplot(111)
ax.plot(true_data label=‘True Data‘)
pyplot.plot(predicted_data label=‘Prediction‘)
pyplot.legend()
pyplot.show()
# load dataset
dataset = read_csv(‘SP500_data.csv‘ header=0 index_col=0)
values = dataset.values
# ensure all data is float
values = values.astype(‘float32‘)
# normalize features
scaler = MinMaxScaler(feature_range=(0 1))
scaled = scaler.fit_transform(values)
time_step = 14
# frame as supervised learning
reframed = series_to_supervised(scaledtime_steptime_step)
print(reframed.head())
input(‘enter‘)
# split into train and test sets
values = reframed.values
print(values)
print(‘len‘len(values))
n_train_days =2983 # train is time_step0%
train = values[:n_train_days :]
test = values[n_train_days: :]
# split into input and out
相关资源
- python +MYSQL+HTML实现21蛋糕网上商城
- 天天生鲜Python版前端页面
- 让Pytohn xhtml2pdf PDF支持中文
- 《机器学习实战》Python3代码
- Long Short Terms Memory Networks with Python J
-
Python第三方库lxm
l最新3.7版本64位系 - python 实现KML转换为Excel的工具
- python机器学习5个数据科学家案例解析
- ml-agents-master
- knn算法识别mnist图片-python3
- [知识图谱实战篇] 六.HTML+D3实现点击节
- MLP/RNN/LSTM模型进行IMDb情感分析
- master machine learning一套书籍
-
lxm
l-4.3.3-cp38-cp38m-win_amd64.whl - python爬取拉勾网存excel+数据可视化w
- python aiml web 聊天机器人
- Python+selenium+HTMLTestRunner+unittest 测试框
- Python-2.7.x-win64-开发扩展包
- 完整python项目,python爬虫 爬取今日头
- python+html桌面应用
- MDNETpython实现
- tensorflow-1.9.0-cp36-cp36m-win_amd64.whl
- 多层感知器实现,机器学习,神经网
- HTMLTestRunnerNew.py
- HTMLTestRunnerCN.py
-
change_xm
l_dafult_folder.py - generate_train_val_test_txt.py
- txt.py python 对比不同的txt文档数据 不
- HTMLTestRunner-基于python3-日志显示-截图
- spark mllib 协同过滤推荐算法ALS) py
评论
共有 条评论