[英]How to reshape multivariate time series data for ConvLSTM2D model
[英]How to reshape a 3D tensor created for multistep and multivariate time series forecasting for Keras to be used in XGBoost?
我在 Keras 中開發了一個 model,它將輸入矩陣 x (168x326) 與 output 向量 y (168x1) 相關聯。 輸入 X 是第i周,包含 168 小時每小時生成的 326 個特征。 Output y 是第i+1周,包含 168 小時價格。 訓練集包含 208 對周 (x_train->y_train),而測試集包含 51 對 (x_test->y_test)。 形狀是 3D 張量,格式如下:
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
*輸出:
x_train:(208、168、326)
y_train: (208, 168, 1)
x_test: (51, 168, 326)
y_test: (51, 168, 1)*
我想使用這些完全相同的數據集來使用 XGBoost 執行價格預測。 我的 model 是這樣構建的:
reg = xgb.XGBRegressor(n_estimators=1000)
reg.fit(x_train, y_train,
eval_set=[(x_train, y_train), (x_test, y_test)],
early_stopping_rounds=50,
verbose=True)
但是,在運行時,我收到一條錯誤消息,指出 XGBoost 需要 2D 向量。 緊隨其后的是:
ValueError: Please reshape the input data into 2-dimensional matrix.
我已經完成了一些刪除或重塑數據集中維度的測試,但我沒有成功。 有人能告訴我如何對數據執行這種轉換嗎? 謝謝。
首先,我需要展平最后兩個維度來創建一個。 我的張量現在具有以下形狀:x_train: (208, 54768), y_train: (208, 168), x_test: (51, 54768) 和 y_test: (51, 168)。 從而將張量從 3D 減少到 2D。 接下來,我發現這些回歸器在默認情況下不適用於多值輸出。 為此,需要導入 MultiOutputRegressor。
from sklearn.multioutput import MultiOutputRegressor
然后你需要在這個包裝器中包含回歸器,如下所示:
reg = MultiOutputRegressor(XGBRegressor())
我對 XGB 和 LGBM 進行了測試,效果很好。 但是,如果您使用的是 CatBoost,最好格式化您的數據以使用 CatBoost 庫自己的池。 這里:
from catboost import Pool
代碼如下所示:
dtrain = Pool(x_train, label=y_train)
params = {'iterations': 500, 'learning_rate': 0.1, 'depth': 3, 'loss_function': 'MultiRMSE'}
CAT_reg = CatBoostRegressor(**params)
CAT_reg.fit(dtrain)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.