简体   繁体   English

管道中SelectKBest方法的问题

[英]Problem with SelectKBest method in pipeline

I am trying to solve a problem where I use KNN algorithm for classification. 我正在尝试解决使用KNN算法进行分类的问题。 While using pipeline, I decided to add SelectKBest but I get the error below : 在使用管道时,我决定添加SelectKBest但出现以下错误:

All intermediate steps should be transformers and implement fit and transform. 所有中间步骤均应为变形器,并执行拟合和转换。

I don't know if I can use this selection algorithm with KNN. 我不知道我是否可以在KNN中使用此选择算法。 But I tried with SVM as well and got the same result. 但是我也尝试使用SVM并获得了相同的结果。 Here is my code : 这是我的代码:

sel = SelectKBest('chi2',k = 3)
clf = kn()
s = ss()
step = [('scaler', s), ('kn', clf), ('sel',sel)]
pipeline = Pipeline(step)
parameter = {'kn__n_neighbors':range(1,40,1), 'kn__weights':['uniform','distance'], 'kn__p':[1,2] }
kfold = StratifiedKFold(n_splits=5, random_state=0)
grid = GridSearchCV(pipeline, param_grid = parameter, cv=kfold, scoring = 'accuracy', n_jobs = -1)
grid.fit(x_train, y_train)

The order of the operations in the pipeline, as determined in steps , matters; steps确定的管道中的操作顺序很重要; from the docs : 文档

steps : list 步骤: 清单

List of (name, transform) tuples (implementing fit/transform) that are chained, in the order in which they are chained, with the last object an estimator. (名称,变换)元组(实现拟合/变换)的列表,按其链接的顺序进行链接,最后一个对象为估计量。

The error is due to adding SelectKBest as the last element of your pipeline: 该错误是由于将SelectKBest添加为管道的最后一个元素造成的:

step = [('scaler', s), ('kn', clf), ('sel',sel)]

which is not an estimator (it is a transformer), as well as to your intermediate step kn not being a transformer. 这不是一个估算器(它是一个变压器),以及您的中间步骤kn不是一个变压器。

I guess you don't really want to perform feature selection after you have fitted the model... 我想您真的不希望安装模型执行功能选择...

Change it to: 更改为:

step = [('scaler', s), ('sel', sel), ('kn', clf)]

and you should be fine. 你应该没事的

So, I didn't think the order of the pipeline is important but then, I found out the last member of the pipeline has to be able to fit/transform. 因此,我认为管道的顺序并不重要,但是随后,我发现管道的最后一个成员必须能够装配/变形。 I changed the order of the pipeline by making clf the last. 我通过使clf为最后一个更改了管道的顺序。 Problem is solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM