Skip to content Skip to sidebar Skip to footer

How To Get Coefficients And Feature Importances From Multioutputregressor?

I am trying to perform a MultiOutput Regression using ElasticNet and Random Forests as follows: from sklearn.ensemble import RandomForestRegressor from sklearn.multioutput import M

Solution 1:

MultiOutputRegressor itself doesn't have these attributes - you need to access the underlying estimators first using the estimators_ attribute (which, although not mentioned in the docs, it exists indeed - see the docs for MultiOutputClassifier). Here is a reproducible example:

from sklearn.multioutput import MultiOutputRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import ElasticNet

# dummy data
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
W = np.array([[1, 1], [1, 1], [2, 2], [2, 2]])

regr_multi_RF=MultiOutputRegressor(RandomForestRegressor())
regr_multi_RF.fit(X,W)

# how many estimators?len(regr_multi_RF.estimators_)
# 2

regr_multi_RF.estimators_[0].feature_importances_
# array([ 0.4,  0.6])

regr_multi_RF.estimators_[1].feature_importances_
# array([ 0.4,  0.4])

regr_Enet = ElasticNet()
regr_multi_Enet= MultiOutputRegressor(regr_Enet)
regr_multi_Enet.fit(X, W)

regr_multi_Enet.estimators_[0].coef_
# array([ 0.08333333,  0.        ])

regr_multi_Enet.estimators_[1].coef_
# array([ 0.08333333,  0.        ])

Solution 2:

regr_multi_Enet.estimators_[0].coef_

To get the coefficients of the first estimator etc.

Post a Comment for "How To Get Coefficients And Feature Importances From Multioutputregressor?"