K最近的邻居第一个应用程序。来自pandas的丢弃函数出错

问题描述 投票:-2回答:1

我试图使用sklearn,numpy和pandas制作我的第一个KNN应用程序。这是我的代码

我查看了熊猫网站,但文档并不精彩。

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

df = pd.read_csv('D:\\Projects\\machine learning\\classification\\K nearest Neighbors\\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, X_test, y_train, y_test = model_selection.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)

这是我的错误:

Traceback (most recent call last):
  File "d:/Projects/machine learning/classification/K nearest Neighbors/main.py", line 9, in <module>
    X = np.array(df.drop(['class'], 0))
  File "C:\Python\lib\site-packages\pandas\core\frame.py", line 3930, in drop
    errors=errors)
  File "C:\Python\lib\site-packages\pandas\core\generic.py", line 3770, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
  File "C:\Python\lib\site-packages\pandas\core\generic.py", line 3802, in _drop_axis
    new_axis = axis.drop(labels, errors=errors)
  File "C:\Python\lib\site-packages\pandas\core\indexes\base.py", line 4910, in drop
    '{} not found in axis'.format(labels[mask]))
KeyError: "['class'] not found in axis"

和“乳腺癌 - 威斯康星.data.txt”:

id, clump_thickness, unif_cell_size, unif_cell_shape, marg_adhesion, single_epidth_cell_size, bare_nuclei, bland_chrom, norm_nucleoli, mitoses, class
1000025,5,1,1,1,2,1,3,1,1,2
1002945,5,4,4,5,7,10,3,2,1,2
1015425,3,1,1,1,2,2,3,1,1,2
......

我不知道出了什么问题。请帮我。谢谢!

python pandas machine-learning knn
1个回答
0
投票

我通过删除txt文件中的所有空格来修复错误。尝试id,clump_thick,unif_cell_size,unif_cell_shape,marg_adhesion,sing_epith_cell_size,bare_nuclei,bland_chromatin,normal_nucl,mitoses,class(不含空格)

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.