我有一个分类问题,我必须使用投票分类器方法找到前3个特征,其中包含PCA,xgboost,随机森林,逻辑注册和决策树。
我是一个初学者,我不知道如何使用投票分类器来获得特征重要性。
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.decomposition import PCA
from sklearn.ensemble import VotingClassifier
log_clf = LogisticRegression(random_state=2)
rnd_clf = RandomForestClassifier
(n_estimators=150, max_depth=3, min_samples_leaf=6,
max_features=0.3, n_jobs=-1, random_state=2)
gbm_clf= GradientBoostingClassifier
(n_estimators=150, max_depth=3, min_samples_leaf=3, max_features=0.3,
learning_rate=0.05, subsample=0.4,random_state=2)`
estimators = [('lr', log_clf), ('rf', rnd_clf), ('gbm', gbm_clf)]
voting_clf = VotingClassifier(estimators=estimators,voting='hard')
voting_clf.fit(train.drop(['target'],1),train['target'])例外:它应该给我使用投票分类器的变量的特征重要性,有pca,xgboost,dt,rf和lr。
发布于 2019-10-30 22:01:32
您可以从voting_clf对象访问底层分类器,并提取这些分类器的特征重要性。例如:
for alg in voting_clf.named_estimators:
clf = voting_clf.named_estimators[alg]
# extract feature importance for clf
# Note different algorithms have different
# methods for feature importance由于你将算法与根本不同的“特征重要性”概念集成在一起,我不认为有一种定义良好的方法来确定哪些特征在集成结果中最重要。
发布于 2021-06-18 17:57:21
我也遇到过同样的问题,然而,罗伯特金的方法并不起作用,因为VotingRegressor (我正在处理回归)有几个带有估计器的字段,而在named_estimators字段中,它们并不适合,因此无法完成特征重要性提取。您可以在第二张图片中看到命名的估计器之一的样子。


具有合适估计器的适当字段是_named_estimators__,它看起来如下所示:

以及获取所有重要信息的代码
def __get_feature_importances(self, train_columns):
feature_imp = dict()
for est in self.model.estimators_:
if type(est) == catboost.core.CatBoostRegressor:
feature_imp['catboost'] = dict(zip(train_columns, est.feature_importances_))
elif type(est) == lightgbm.sklearn.LGBMRegressor:
feature_imp['lgbm'] = dict(zip(train_columns, est.feature_importances_))
elif type(est) == xgboost.sklearn.XGBRegressor:
feature_imp['xgboost'] = dict(zip(train_columns, est.feature_importances_))
return feature_imp我们必须按类型比较它们,因为_named_estimators__没有名称。
https://stackoverflow.com/questions/58626696
复制相似问题