import pandas as pd
from sklearn.metrics import mean_squared_error
import math
import matplotlib.pyplot as plt
import datetime
# 加载数据
def parser(x):
return datetime.datetime.strptime(x, '%Y/%m/%d')
ser = pd.read_csv('../LSTM系列/LSTM单变量1/data_set/shampoo-sales.csv',
header=0, parse_dates=[0], index_col=0, date_parser=parser).squeeze('columns')
# 分成训练集和测试集合,前24行给训练集,后12行给测试集
X = ser.values
train, test = X[:-12], X[-12:]
history = [x for x in train]
predictions = []
for i in range(len(test)):
predictions.append(history[-1]) # history[-1],就是执行预测,这里我们只是假设predictions数组就是我们预测的结果
history.append(test[i]) # 将新的测试数据加入模型
# 预测效果评估
rmse = math.sqrt(mean_squared_error(test, predictions))
print('RMSE:%.3f' % rmse)
# 画出预测+观测值
plt.plot(test)
plt.plot(predictions)
plt.show()