【问题标题】:Access actual Features after a Feature Selection Pipeline in SciKit-Learn在 SciKit-Learn 中的特征选择管道之后访问实际特征
【发布时间】:2016-11-30 09:19:11
【问题描述】:

我在 SciKit-Learn 中将特征选择与管道结合使用。作为特征选择策略,我使用SelectKBest

管道的创建和执行如下:

select = SelectKBest(k=5)
clf = SVC(decision_function_shape='ovo')
    parameters = dict(feature_selection__k=[1,2,3,4,5,6,7,8], 
              svc__C=[0.01, 0.1, 1],
              svc__decision_function_shape=['ovo'])
steps = [('feature_selection', select),
                 ('svc', clf)]
pipeline = sklearn.pipeline.Pipeline(steps)
cv = sklearn.grid_search.GridSearchCV(pipeline, param_grid=parameters)
cv.fit( features_training, labels_training )

我知道之后我可以通过cv.best_params_ 获得最佳参数。但是,这只告诉我k=4 是最佳的。但我想知道这些是哪些功能?如何做到这一点?

【问题讨论】:

    标签: python machine-learning scikit-learn feature-detection


    【解决方案1】:

    对于您的示例,您可以使用cv.best_estimator_.named_steps['feature_selection'].scores_ 获取所有功能的分数。这将为您提供所有功能的分数,并且使用它们您应该能够看到哪些是所选功能。同样,您也可以通过cv.best_estimator_.named_steps['feature_selection'].pvalues_ 获取pvalues。

    编辑

    更好的方法是使用SelectKBest 类的get_support 方法。这将给出一个形状为[# input features] 的布尔数组,其中一个元素为True,前提是选择了其对应的特征进行保留。这将如下所示:

    cv.best_estimator_.named_steps['feature_selection'].get_support()

    【讨论】:

    • 不错。所以如果.scores_ 的结果是,例如[ 891.65675063 952.43574853 739.36567492 913.33581205 753.59383098 910.65470991 867.7711945 469.26835899],我从best_params_ 看到k=4,那么我可以假设,具有最高值的4 个特征被选中?这是正确的吗?
    • 我认为这是正确的。请检查我对答案的最新编辑。这是查看所选功能的最佳方式。
    猜你喜欢
    • 2018-02-24
    • 2018-06-01
    • 2015-09-08
    • 2017-02-10
    • 1970-01-01
    • 2014-05-22
    • 2019-02-14
    • 2021-03-26
    • 2014-11-05
    相关资源
    最近更新 更多