XGBoosting Home | About | Contact | Examples

XGBoost for Multiple-Output Regression with MultiOutputRegressor

When dealing with multiple output regression tasks (multi-out regression), where the goal is to predict multiple continuous target variables simultaneously, XGBoost can be combined with scikit-learn’s MultiOutputRegressor to create a powerful and efficient solution.

This example demonstrates how to train an XGBoost model for multiple output regression using the MultiOutputRegressor wrapper from scikit-learn.

We’ll generate a synthetic dataset, prepare the data, initialize the model, train it, and evaluate its performance.

# XGBoosting.com
# Train an XGBoost Model for Multiple Output Regression using MultiOutputRegressor
from xgboost import XGBRegressor
from sklearn.datasets import make_regression
from sklearn.multioutput import MultiOutputRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Generate a synthetic multi-output regression dataset
X, y = make_regression(n_samples=1000,
                       n_features=10,
                       n_targets=3,
                       noise=0.1,
                       random_state=42,
                       n_informative=5)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize an XGBRegressor model
base_model = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)

# Wrap the XGBRegressor with MultiOutputRegressor
model = MultiOutputRegressor(base_model)

# Fit the MultiOutputRegressor on the training data
model.fit(X_train, y_train)

# Make predictions on the test set
y_pred = model.predict(X_test)

# Evaluate the model's performance using mean squared error
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")

Here’s how it works:

  1. Generate a synthetic multi-output regression dataset with 10 input features and 3 output targets.
  2. Split the data into training and testing sets using train_test_split.
  3. Initialize an XGBRegressor model with chosen hyperparameters.
  4. Wrap the XGBRegressor with MultiOutputRegressor to handle multiple outputs.
  5. Fit the MultiOutputRegressor on the training data using fit().
  6. Make predictions on the test set using predict().
  7. Evaluate the model’s performance using Mean Squared Error (MSE).

By using XGBoost with MultiOutputRegressor, you can effectively handle multiple output regression tasks, leveraging the power and efficiency of XGBoost while seamlessly working with multiple target variables.

This example serves as a starting point for training XGBoost models for multi-output regression. Depending on your specific dataset and requirements, you may need to preprocess the data, tune hyperparameters, or use different evaluation metrics to achieve optimal results.



See Also