SKLearner Home | About | Contact | Examples

Scikit-Learn MultiOutputRegressor Model

MultiOutputRegressor is a meta-estimator for performing multi-target regression, fitting one regressor per target variable. It is suitable for regression problems with multiple continuous target variables.

The MultiOutputRegressor takes a base estimator as an argument and fits one estimator per target. There are no additional hyperparameters beyond those of the base estimator.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.multioutput import MultiOutputRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

# generate multi-target regression dataset
X, y = make_regression(n_samples=100, n_features=5, n_targets=3, random_state=1)

# split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

# create model
model = MultiOutputRegressor(estimator=LinearRegression())

# fit model
model.fit(X_train, y_train)

# evaluate model
yhat = model.predict(X_test)
mae = mean_absolute_error(y_test, yhat)
print('MAE: %.3f' % mae)

# make a prediction
row = [[0.21947749, 0.32948997, 0.81560036, 0.440956, -0.0606303]]
yhat = model.predict(row)
print('Predicted: %s' % yhat[0])

Running the example gives an output like:

MAE: 0.000
Predicted: [55.50072876 47.40679794 61.47259778]

The steps are as follows:

  1. A synthetic multi-target regression dataset is generated using make_regression() with a specified number of samples (n_samples) and target variables (n_targets). The dataset is split into training and test sets using train_test_split().

  2. A MultiOutputRegressor is instantiated with a base estimator, in this case, LinearRegression(). The model is then fit on the training data using the fit() method.

  3. The performance of the model is evaluated by comparing the predictions (yhat) to the actual values (y_test) using the mean absolute error metric.

  4. A single prediction can be made by passing a new data sample to the predict() method, which returns predictions for all target variables.

This example demonstrates how to use MultiOutputRegressor for multi-target regression problems in scikit-learn. By leveraging a base estimator, it simplifies the process of fitting multiple regressors for each target variable.



See Also