我正在尝试使用带有SelectPercentile和RandomForestClassifier的管道来匹配特性得分和列名。 从我最初的EDA来看,特性重要性分析似乎是有意义的。然而,我不确定我是否做对了。 我主要关心的是通过SelectPercentile获得原始的DataFrame列索引id(以了解原始的列名)。并假设这些特征与RandomForestClassifier.feature_importances_对齐(按顺序排列) 我从没见过有人这么做。我做的对吗? 我的管道:
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
# NOTE: Make sure that the outcome column is labeled 'target' in the data file
tpot_data = df_delays_final_ml
features = tpot_data.drop('class', axis=1)
training_features, testing_features, training_target, testing_target = \
train_test_split(features, tpot_data['class'], random_state=None)
# Average CV score on the training set was: 0.9131840089838837
exported_pipeline = make_pipeline(
SelectPercentile(score_func=f_classif, percentile=56),
RandomForestClassifier(bootstrap=False, criterion="entropy", max_features=0.9000000000000001, min_samples_leaf=9, min_samples_split=9, n_estimators=100)
)
exported_pipeline.fit(training_features, training_target)
results = exported_pipeline.predict(testing_features)
特征分析位:
nbr_of_features = exported_pipeline.named_steps['selectpercentile'].get_support().sum()
top_features = [tpot_data.columns.tolist()[i] for i in np.argsort(exported_pipeline.named_steps['selectpercentile'].scores_)[::-1]][0:nbr_of_features]
importances = exported_pipeline.named_steps['randomforestclassifier'].feature_importances_
std = np.std([tree.feature_importances_ for tree in exported_pipeline.named_steps['randomforestclassifier'].estimators_], axis=0)
indices = np.argsort(importances)[::-1]
col_arr = []
for f in range(nbr_of_features):
col_name = top_features[f]
col_arr.insert(len(col_arr), col_name)
print("%d. feature %d (%f) [%s]" % (f + 1, indices[f], importances[indices[f]], col_name))
plt.figure(figsize=(19, 6))
plt.title("Feature importances")
plt.bar(range(nbr_of_features), importances[indices], color="r", yerr=std[indices], align="center")
plt.xticks(range(nbr_of_features), col_arr, rotation=45)
plt.xlim([-1, 9])
plt.show()
结果:
问题来源StackOverflow 地址:/questions/59381782/extracting-feature-detail-from-selectpercentile-column-names-and-respective-sco
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。