I am trying to perform a MultiOutput Regression using ElasticNet and Random Forests as follows:
from sklearn.ensemble import RandomForestRegressor
from sklearn.multioutput import MultiOutputRegressor
from sklearn.linear_model import ElasticNet
X_train, X_test, y_train, y_test = train_test_split(X_features, y, test_size=0.30,random_state=0)
Elastic Net
l1_range=np.arange(0.1,1.05,0.1).tolist()
regr_Enet=ElasticNetCV(cv=5,copy_X=True,n_alphas=100,l1_ratio=l1_range,selection='cyclic',normalize=False,verbose =2,n_jobs=1)
regr_multi_Enet= MultiOutputRegressor(regr_Enet)##ElasticNetCV
regr_multi_Enet.fit(X_train, y_train)
Random Forest
max_depth = 20
number_of_trees=100
regr_multi_RF=MultiOutputRegressor(RandomForestRegressor(n_estimators=number_of_trees,max_depth=max_depth,random_state=0,n_jobs=1,verbose=1))
regr_multi_RF.fit(X_train, y_train)
y_multirf = regr_multi_RF.predict(X_test)
Everything is going well, however I haven't found a way to obtain the coefficients (coef_ ) or most important features (feature_importances_) of the model. When I write:
regr_multi_Enet.coef_
regr_multi_RF.feature_importances_
It shows the following error:
AttributeError: 'MultiOutputRegressor' object has no attribute 'feature_importances_'
AttributeError: 'MultiOutputRegressor' object has no attribute 'coef_'
I have read the documentation on MultiOutputRegressor but I cannot find a way to extract the coefficients. Anyone knows how to retrieve them?
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:To get the coefficients of the first estimator etc.