Search code examples
pythondataframescikit-learnregressionxgboost

Predicted value stay constant XGBRegressor


For illustration purpose, I want to forecast exchange rate. From the csv file enclosed and I created 2 columns for prior 1 month and prior 2 month. After performing train_test_split, I use XGBRegressor to forecast price.

Surprisingly, the predicted value turns out almost constant.

Excel data file link (pls click on the download icon): https://easyupload.io/1kf2hg

My code are enclosed.

import xgboost as xgb
from sklearn.model_selection import train_test_split

import pandas as pd

df = pd.read_csv(r'rate.csv')
df['prior_1_month'] = df['price'].shift(1)
df['prior_2_month'] = df['price'].shift(2)
df=df.dropna()

x = df[['prior_1_month','prior_2_month']]
y = df["price"]

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=False)

# print(X_train[:3]);print(X_test[:3]);print(y_train[:3]);print(y_test[:3])

reg = xgb.XGBRegressor(n_estimators=1000)
reg.fit(X_train[[ 'prior_1_month', 'prior_2_month']], y_train, verbose = True)
# xgb.plot_importance(reg)

X_test['predicted_value_ML_1'] = reg.predict(X_test)
print(X_test)
y_test

Solution

  • Try to use booster='gblinear' parameter.

    reg = xgb.XGBRegressor(booster='gblinear')
    

    The predicted value stay constant because input data is sample and using tree-based regression to predict. Using a linear routine could solve it.