python - 如何在拟合我的估计器之前使用 sklearn RFECV 选择最佳特征以传递给降维步骤
问题描述
在使用 KNN 拟合我的估计器之前,如何使用 sklearn RFECV 方法选择最佳特征以传递给 LinearDiscriminantAnalysis(n_components=2) 方法进行降维。
pipeline = make_pipeline(Normalizer(), LinearDiscriminantAnalysis(n_components=2), KNeighborsClassifier(n_neighbors=10))
X = self.dataset
y = self.postures
min_features_to_select = 1 # Minimum number of features to consider
rfecv = RFECV(svc, step=1, cv=None, scoring='f1_weighted', min_features_to_select=min_features_to_select)
rfecv.fit(X, y)
print(rfecv.support_)
print(rfecv.ranking_)
print("Optimal number of features : %d" % rfecv.n_features_)
Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(min_features_to_select,
len(rfecv.grid_scores_) + min_features_to_select),
rfecv.grid_scores_)
plt.show()
我从此代码中收到以下错误。如果我在没有 LinearDiscriminantAnalysis() 步骤的情况下运行此代码,那么它可以工作,但这是我处理的重要部分。
*** ValueError: when `importance_getter=='auto'`, the underlying estimator Pipeline should have `coef_` or `feature_importances_` attribute. Either pass a fitted estimator to feature selector or call fit before calling transform.
解决方案
您的方法有一个整体问题:KNeighborsClassifier
没有对特征重要性的内在度量。RFECV
因此,它与有关分类器的文档所述不兼容:
具有拟合方法的监督学习估计器,通过 coef_ 属性或通过 feature_importances_ 属性提供有关特征重要性的信息。
你肯定会失败KNeighborsClassifier
。您肯定需要另一个分类器,例如RandomForestClassifier
or SVC
。
如果您可以选择另一个分类器,您的管道仍然需要公开估计器在您的管道中的特征重要性。为此,您可以在此处参考此答案,该答案为此目的定义了一个自定义管道:
class Mypipeline(Pipeline):
@property
def coef_(self):
return self._final_estimator.coef_
@property
def feature_importances_(self):
return self._final_estimator.feature_importances_
定义您的管道,如:
pipeline = MyPipeline([
('normalizer', Normalizer()),
('ldm', LinearDiscriminantAnalysis(n_components=2)),
('rf', RandomForestClassifier())
])
它应该可以工作。
推荐阅读
- dvc - 没有导入的派生数据的 DVC 依赖项
- python - 如何使用 Python 将 RGB565 字节数组转换为 RGB888 字节数组?
- c# - 如何首先打开一个窗口,而不是 WPF 中的 MainWindow(C#)
- javascript - 如何在javascript中的字符串内创建嵌套for循环
- metabase - 对远程服务器进行 REST API 调用 - 元数据库
- asp.net - ASP.NET 文件上传不会在更新面板中保留回发文件
- flutter - Flutter AnimatedList 与提供者模式
- android - HorizontalScrollView 和 swipe 在 android 中一次不起作用
- regex - Notepad++ 正则表达式在 XML 中查找与某个值不匹配的结果
- amazon-web-services - AWS ec2 实例在 dockerfile 中的 I/O 繁重操作后变得无响应