使用KNeighbors分类器的SKLearning管道

SKlearn pipeline using KNeighborsClassifier(使用KNeighbors分类器的SKLearning管道)
本文介绍了使用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管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)