Training the XGBoost model
With the data prepared, we can now train the XGBoost model. For time series forecasting, XGBoost typically uses the regression objective (reg:squarederror
), which optimizes for minimizing squared errors. First, we will initialize the XGBoost model and train it on the training data. Then, we can use the model to make predictions on the test set. Let’s start:
- Begin by importing
XGBRegressor
fromxgboost
and initializing the regressor with the objective ofreg:squarederror
. Start withn_estimators = 100
,max_depth =3
, andlearning_rate = 0.1
. These are conservative settings compared to the default XGBoost values, which will prevent overfitting of the data. You can tune these hyperparameters later to suit the project you are doing. Details on the XGBoost hyperparameters are in Chapter 5:from xgboost import XGBRegressor model = XGBRegressor(objective='reg:squarederror', Â Â Â Â n_estimators=100, max_depth=3, learning_rate...