【问题标题】:Why I am getting this value error in KNN model?为什么我在 KNN 模型中得到这个值错误?
【发布时间】:2018-06-26 16:10:27
【问题描述】:

我在威斯康星州乳腺癌数据上应用 KNN 模型,但每次运行代码时都会出现此错误:

ValueError: 发现样本数量不一致的输入变量:[559, 140]

import numpy as np
import pandas as pd
from sklearn import preprocessing,cross_validation,neighbors

df=pd.read_csv('breast-cancer-wisconsin.data.txt')
df.replace('?',-99999,inplace=True)
df.drop(['id'],1,inplace=True)

X=np.array(df.drop(['class'],1))
y=np.array(df['class'])

X_train, y_train, X_test, y_test = cross_validation.train_test_split(X, y, test_size=0.2)

clf = neighbors.KNeighborsClassifier()
clf.fit(X_train, y_train)
accuracy=clf.score(X_test, y_test)
print(accuracy)

example=np.array([4,2,1,1,1,2,3,2,1])
example=example.reshape(-1,1)

prediction=clf.predict(example)
print(prediction)

【问题讨论】:

  • 请提出您的问题reproducible,包括数据的可下载链接,以及您可能已经完成的任何额外预处理(可能用于插入列名)

标签: python pandas scikit-learn


【解决方案1】:

cross_validation.train_test_split 的输出,根据documentation,应该是X_train, X_test, y_train, y_test。将代码中的该行更改为:

X_train,X_test,y_train,y_test=cross_validation.train_test_split(X,y,test_size=0.2)

【讨论】:

猜你喜欢
  • 2016-11-25
  • 2017-09-18
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
相关资源
最近更新 更多