本文介绍了使用KNeighbors分类器的SKLearning管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用KNeighbors分类器和支持向量机在sklear中构建一个GridSearchCV管道。到目前为止,我已经尝试了以下代码:
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=3)
from sklearn import svm
from sklearn.svm import SVC
clf = SVC(kernel='linear')
pipeline = Pipeline([ ('knn',neigh), ('sVM', clf)]) # Code breaks here
weight_options = ['uniform','distance']
param_knn = {'weights':weight_options}
param_svc = {'kernel':('linear', 'rbf'), 'C':[1,5,10]}
grid = GridSearchCV(pipeline, param_knn, param_svc, cv=5, scoring='accuracy')
但我收到以下错误:
TypeError: All intermediate steps should be transformers and implement fit and transform. 'KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=1, n_neighbors=3, p=2,
weights='uniform')' (type <class 'sklearn.neighbors.classification.KNeighborsClassifier'>) doesn't
谁能帮帮我,我哪里做错了,怎么改正?我认为最后一行也有问题,re parms。
推荐答案
错误清楚地表明KNeighbors分类器没有转换方法KNN只有FIT方法,而AS SVM有FIT_Transform()方法。对于管道,我们可以向它传递n个参数。但是所有的参数都应该有转换器方法。请参考下面的链接
http://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html
这篇关于使用KNeighbors分类器的SKLearning管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!