资源简介
时间序列预测讲义(ARIMA&LSTM;)及python代码,首先讲述了基本概念及公式,然后提供了python代码
代码片段和文件信息
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import DenseActivationdropout
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras import optimizers
import time
plt.style.use(‘ggplot‘)
# convert an array of values into a dataset matrix
def create_dataset(dataset look_back=1):
dataX dataY = [] []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back)]
dataX.append(a)
dataY.append(dataset[i + look_back])
return numpy.array(dataX) numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv(‘soybean_price_guangdong.csv‘header=0parse_dates=[0]index_col=0squeeze=True)
dataset = dataframe.values
dataset = dataset.astype(‘float32‘)
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.7)
test_size = len(dataset) - train_size
train test = dataset[0:train_size] dataset[train_size:len(dataset)]
# reshape into X=t and Y=t+1
look_back = 1
trainX trainY = create_dataset(train look_back)
testX testY = create_dataset(test look_back)
# reshape input to be [samples time steps features]
trainX = numpy.reshape(trainX (trainX.shape[0] 1 trainX.shape[1]))
testX = numpy.reshape(testX (testX.shape[0] 1 testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(input_dim=1output_dim=50return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(100return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(output_dim=1))
model.add(Activation(‘linear‘))
start=time.time()
model.compile(loss=‘mean_squared_error‘optimizer=‘Adam‘)
print (‘compilation time:‘time.time()-start)
history=model.fit(trainXtrainYbatch_size=1nb_epoch=100validation_split=0.1verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0] trainPredict[:0]))
print(‘Train Score: %.2f RMSE‘ % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0] testPredict[:0]))
print(‘Test Score: %.2f RMSE‘ % (testScore))
# shift train predictions for plotting
trainPredictPlot=numpy.empty_like(dataset)
trainPredictPlot[:]=numpy.nan
trainPredictPlot=numpy.reshape(trainPredictPlot(dataset.shape[0]1))
trainPredictPlot[look_back:len(trainPredict)+look_back :] = trainPredict
# shift test predictions for plot
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1406714 2018-05-24 10:34 39 第47讲\ARIMA\.ipynb_checkpoints\soybean_price_guangdong_ARIMA-checkpoint.ipynb
文件 21735 2018-05-24 10:33 39 第47讲\ARIMA\soybean_price_guangdong.csv
文件 903635 2018-05-24 10:33 39 第47讲\ARIMA\soybean_price_guangdong_ARIMA.ipynb
文件 250664 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\.ipynb_checkpoints\soybean_price_guangdong_LSTM-checkpoint.ipynb
文件 20833 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\loss_log.png
文件 92615 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\result.png
文件 21735 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\soybean_price_guangdong.csv
文件 3560 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\soybean_price_guangdong.py
文件 250664 2018-05-24 10:33 39 第47讲\LSTM\预测一天的结果\soybean_price_guangdong_LSTM.ipynb
文件 21735 2018-05-24 10:32 39 第47讲\soybean_price_guangdong.csv
文件 42653 2018-05-24 10:32 39 第47讲\soybean_price_guangdong.xlsx
文件 1087885 2018-05-24 10:34 39 第47讲\weather-prophet-master\.ipynb_checkpoints\forecast-temperatures-using-prophet-checkpoint.ipynb
文件 1087885 2018-05-24 10:33 39 第47讲\weather-prophet-master\forecast-temperatures-using-prophet.ipynb
文件 407274 2018-05-24 10:33 39 第47讲\weather-prophet-master\outdoor-temperature-hourly.csv
文件 514619 2018-05-24 10:33 39 第47讲\weather-prophet-master\outdoor-temperature-hourly_20170710.csv
文件 919851 2018-05-24 10:33 时间序列(ARIMA&LSTM)PPT.pdf
文件 1124283 2018-06-13 17:09 时间序列预测讲义.docx
目录 0 2018-05-24 10:45 39 第47讲\LSTM\预测一天的结果\.ipynb_checkpoints
目录 0 2018-05-24 10:45 39 第47讲\ARIMA\.ipynb_checkpoints
目录 0 2018-05-24 10:45 39 第47讲\LSTM\预测一天的结果
目录 0 2018-05-24 10:45 39 第47讲\weather-prophet-master\.ipynb_checkpoints
目录 0 2018-05-24 10:45 39 第47讲\ARIMA
目录 0 2018-05-24 10:45 39 第47讲\LSTM
目录 0 2018-05-24 10:45 39 第47讲\weather-prophet-master
目录 0 2018-06-13 18:05 39 第47讲
----------- --------- ---------- ----- ----
8178340 25
评论
共有 条评论