python - 为多元预测创建 LSTM 模型时遇到问题
问题描述
数据:天气数据集
到目前为止,我只在数据框中选择了三个参数。
输入:
df.info()
输出:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 78888 entries, 2006-01-01 00:00:00 to 2014-12-31 23:00:00
Freq: H
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Temperature (C) 78888 non-null float64
1 Humidity 78888 non-null float64
2 Visibility (km) 78888 non-null float64
dtypes: float64(3)
memory usage: 2.4 MB
参数图之一如下。
df['Temperature (C)'].plot(figsize=(30,8))
首先,我想将 Dataframe 从每小时数据更改为每月数据,这样训练起来会更容易。
df = df.resample('MS').mean()
像这样,
几个月的温度变化是,
df['Temperature (C)'].plot(figsize=(30,8))
测试和训练:
rows_per_month=1
test_months = 18 #number of months we want to predict in the future.
test_indices = test_months*rows_per_month
test_indices
# train and test split:
train = df_final.iloc[:-test_indices]
# Choose the variable/parameter you want to predict
test = df_final.iloc[-test_indices:]
MinMaxScaler
我使用from sci-kit learn对数据进行了缩放
发电机参数:
length = 12*rows_per_month #Length of output sequences (in number of timesteps)
batch_size = 1 #Number of timeseries sample in batch
generator = tf.keras.preprocessing.sequence.TimeseriesGenerator(scaled_train,scaled_train,length=length,batch_size=batch_size)
模型df
(以月为单位):
# define model
model = Sequential()
model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
model.add(tf.keras.layers.LSTM(50))
#NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
model.add(Dense(scaled_train.shape[1]))
model.compile(optimizer='adam', loss='mse')
该模型训练了 24 个 epoch,并且在预测以下三个参数方面做得相当好。
模型预测(何时df
为每月):
问题是当我增加数据的密度并将其设置为每天而不是每月时。
我使用了原始数据并执行了以下操作:
df = df.resample('D').mean()
日内气温变化:
df['Temperature (C)'].plot(figsize=(30,8))
测试和训练:
这里唯一改变的是rows_per_month = 30
,其余一切都一样。
发电机参数:
也和上面一样。
型号df
(天):
相同的模型(就像我df
在几个月内使用的那样)
model = Sequential()
model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
model.add(tf.keras.layers.LSTM(50))
# model.add(tf.keras.layers.LSTM(50)) #add this layer if df is in 'days'
#NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
model.add(Dense(scaled_train.shape[1]))
model.compile(optimizer='adam', loss='mse')
该模型训练了 24 个 epoch,但模型没有正确预测。
损失:
模型预测(df
每天何时)
我尝试再添加一层50 个 LSTM单元。
model = Sequential()
model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
model.add(tf.keras.layers.LSTM(50, return_sequences=True))
model.add(tf.keras.layers.LSTM(50)) #add this layer if df is in 'days'
#NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
model.add(Dense(scaled_train.shape[1]))
model.compile(optimizer='adam', loss='mse')
但结果相似。
我还尝试将模型训练更多的时期(〜100),但没有得到结果。
我想我错过了一个关键点,数据的周期性保持不变,只是点的密度发生了变化,为什么这会影响模型的准确性?
- 如何从预测每日参数的模型中获得不错的准确性?以及每小时的数据?
解决方案
推荐阅读
- ios - 如何使用 swift 作为邮递员请求测试使用行 http 正文进行发布请求?
- reactjs - 将对象传递给 console.warn 导致 react-native 错误
- c# - 如何设置 NLog 在 Xamarin Forms 上创建每小时日志文件?
- angular - Angular 6 Boilerplate 只有基本功能(一直需要)?
- android - 将图像发送到另一个活动
- vb.net - 组合框默认值,vb.net
- android - 无法使用 buildozer 构建 APK 文件。Unicode解码错误
- sql - 在多表多数据库中选择
- firebase - 错误 - 使用 ionic v3 更新 firebase 数据库中的用户名
- go - 如何解决“未知转义序列(以及另外 2 个错误)”的错误