如何从MultiOutputRegressor获取系数和特征重要性? [英] How to get coefficients and feature importances from MultiOutputRegressor?
问题描述
我正在尝试使用ElasticNet和Random Forests执行多输出回归,如下所示:
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)
弹性网
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)
随机森林
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)
一切进展顺利,但是我还没有找到一种方法来获取模型的系数(coef_)或最重要的特征(feature_importances_).当我写的时候:
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_
它显示以下错误:
AttributeError: 'MultiOutputRegressor' object has no attribute 'feature_importances_'
AttributeError: 'MultiOutputRegressor' object has no attribute 'coef_'
我已经阅读了MultiOutputRegressor上的文档,但是我找不到提取系数的方法.有人知道如何找回它们吗?
I have read the documentation on MultiOutputRegressor but I cannot find a way to extract the coefficients. Anyone knows how to retrieve them?
推荐答案
MultiOutputRegressor本身不具有这些属性-您需要首先使用estimators_
属性(尽管在文档,它确实存在-请参阅 MultiOutputClassifier ).这是一个可重现的示例:
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. ])
这篇关于如何从MultiOutputRegressor获取系数和特征重要性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!