python - 在 Python 中使用 scaler.inverse_transform 时,值错误操作数无法与形状一起广播
问题描述
我在我的代码的最后一部分,我遇到了一个无法与形状一起广播的操作数 (69331,4) (3,) (69331,4)。
values = df.values
## full data without resampling
#values = df.values
# integer encode direction
# ensure all data is float
#values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# frame as supervised learning
reframed = series_to_supervised(scaled, 1, 1)
# drop columns we don't want to predict
reframed.drop(reframed.columns[[0]], axis=1, inplace=True)
print(reframed.head())
这是我拆分数据的方式:
# split into train and test sets
values = reframed.values
n_train_time = 29713
train = values[:n_train_time, :]
test = values[n_train_time:, :]
##test = values[n_train_time:n_test_time, :]
# split into input and outputs
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
# We reshaped the input into the 3D format as expected by LSTMs, namely [samples, timesteps, features].
这是我制作模型的方法:
model = Sequential()
model.add(LSTM(100, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dropout(0.2))
# model.add(LSTM(70))
# model.add(Dropout(0.3))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
以下是我适合我的网络的方式:
history = model.fit(train_X, train_y, epochs=20, batch_size=70, validation_data=(test_X, test_y), verbose=2, shuffle=False)
以下是我做出预测的方式:
# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], 4))
# invert scaling for forecast
inv_yhat = np.concatenate((yhat, test_X[:,-3:]), axis=1)
这是我的错误发生的地方:
inv_yhat = scaler.inverse_transform(inv_yhat)
错误是:
ValueError: operands could not be broadcast together with shapes (69331,4) (3,) (69331,4)
如果我做:
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
我得到:
(29713, 1, 4) (29713,) (69331, 4) (69331, 1)
任何帮助将不胜感激
解决方案
推荐阅读
- powershell - 来自 Get-PnPListItem 的 powershell 下载文件
- java - 带有 JDA 的 Maven 阴影插件
- python - 气流找不到本地文件
- kubernetes - 主 Kubernetes 节点离线 GKE(多个集群和项目)
- swift - 菜单标题问题和不透明度降低
- tensorflow - 使用 savedmodel 导出 TensorFlow 实验模型
- bash - 使用变量作为另一个变量名的一部分
- php - 在准备语句时参数解包后不能使用位置参数
- python - 尽管明确声明为 dtype=object,pd.to_csv 仍将混合的 int/str 内容导出为 float
- c# - DataTemplate 中的 Xamarin.Forms CollectionView 项目“背后”是什么(关于背景颜色)?