How to fetch R2 for each target in sklearn MultiOutputRegressor() rather than the overall R2?

Viewed 170

For example, Xs has 5 independent variables, and Ys has 5 dependent variables:

x_train, x_test, y_train, y_test = train_test_split(Xs, Ys, test_size=0.2, random_state=2)

model = lgb.LGBMRegressor()
wrapper = MultiOutputRegressor(model)

model.fit(x_train, y_train)
model.score(x_test, y_test)

Could only get the overall R2 through the code above, what if I want to check the R2 for each Y? Is it possible?

Thanks

1 Answers

You can use scikit-learn r2_score with multioutput='raw_values':

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.multioutput import MultiOutputRegressor
from sklearn.metrics import r2_score
import lightgbm as lgb

# generate the data
X, Y = make_regression(n_targets=5, n_features=10, n_samples=1000, random_state=42)

# split the data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)

# instantiate the model
model = MultiOutputRegressor(estimator=lgb.LGBMRegressor())

# fit the model
model.fit(X_train, Y_train)

# generate the model predictions
Y_pred = model.predict(X_test)

# calculate the individual R2's
print(r2_score(Y_test, Y_pred, multioutput='raw_values'))
# [0.907924 0.925267 0.906492 0.939653 0.881619]

print([r2_score(Y_test[:, i], Y_pred[:, i]) for i in range(Y_test.shape[1])])
# [0.907924, 0.925267, 0.906492, 0.939653, 0.881619]

# calculate the overall R2
print(model.score(X_test, Y_test))
# 0.9121908184618046

print(r2_score(Y_test, Y_pred, multioutput='uniform_average'))
# 0.9121908184618046
Related