开发者社区> 问答> 正文

从SelectPercentile中提取特征细节:列名和相应的分数

我正在尝试使用带有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

展开
收起
kun坤 2019-12-27 17:45:41 1548 0
1 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载