我有一个我无法解决的问题,我真的不明白发生了什么。
我有原始数据集lrdata4
year total_vehicles
0 2000 419587299
1 2001 425832533
2 2002 430480581
3 2003 434270003
4 2004 442680113
from statsmodels.tools.eval_measures import rmse
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
import warnings
warnings.filterwarnings('ignore')
当我这样做时
lrdata4.year = pd.to_datetime(lrdata4.year)
lrdata4 = lrdata4.set_index('year')
lrdata4.tail(10)
岁月改变了,我不知道为什么。有帮助吗?
total_vehicles
year
1970-01-01 00:00:00.000002009 443333980
1970-01-01 00:00:00.000002010 438827716
1970-01-01 00:00:00.000002011 440461505
1970-01-01 00:00:00.000002012 440073277
1970-01-01 00:00:00.000002013 441751395
1970-01-01 00:00:00.000002014 451394270
1970-01-01 00:00:00.000002015 460050397
1970-01-01 00:00:00.000002016 470256985
1970-01-01 00:00:00.000002017 474693803
1970-01-01 00:00:00.000002018 473765568
因此,我正在准备模型以进行时间序列预测
train, test = lrdata4[:-12], lrdata4[-12:]
enter code herescaler = MinMaxScaler()
scaler.fit(train)
train = scaler.transform(train)
test = scaler.transform(test)
n_input = 24
n_features = 1
generator = TimeseriesGenerator(train, train, length = n_input, batch_size =1000)
model = Sequential()
model.add(LSTM(200, activation= 'relu', input_shape=(n_input, n_features)))
model.add(Dropout(0.15))
model.add(Dense(1))
model.compile(optimizer='adam', loss ='mse')
model.fit_generator(generator, epochs=20)
我得到了预测,并将它们附加到列表中
pred_list = []
batch = train[-n_input:].reshape((1 ,n_input, n_features))
for i in range(n_input):
pred_list.append(model.predict(batch)[0])
batch = np.append(batch[:, 1:,:],[[pred_list[i]]], axis=1)
而且我正在尝试合并我的原始数据帧lrdata4和新的数据帧df_predict
df_predict = pd.DataFrame(scaler.inverse_transform(pred_list), index= lrdata4[-n_input:].index,columns=['Predictions'])
df_test = pd.concat([lrdata4, df_predict], axis = 1)
但是连环画永远长存。我把它放了四个小时,它还在继续。是因为我的原始数据集有20万个数据吗?还有另一种方法吗?
我已经尝试过像这样绘制不同的图
plt.figure(figsize= (20,5))
plt.plot(lrdata4.index, lrdata4['total_vehicles'])
plt.plot(df_predict.index, df_predict['Predictions'], color ='r')
plt.show()
但是该图不是带有原始数据和预测数据的折线图,而是连续的折线图,年份错误,我不明白为什么This is how the graph looks like
这里的年份是整数-因此尝试转换为日期将无效。如果您确实希望将其设置为日期时间,则可以尝试使用类似方法转换年份;
df['year'] = df['year'].apply(lambda x: '{}-01-01'.format(x))
然后使用pd.to_datetime
转换为日期时间。