我尝试使用 sklearn、numpy 和 pandas 制作我的第一个 KNN 应用。这是我的代码
我查看了 pandas 的网站,但文档并不出色。
import numpy as np from sklearn import preprocessing, model_selection, neighborsimport 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"
这是 “breast-cancer-wisconsin.data.txt” 文件内容:
id, clump_thickness, unif_cell_size, unif_cell_shape, marg_adhesion, single_epidth_cell_size, bare_nuclei, bland_chrom, norm_nucleoli, mitoses, class1000025,5,1,1,1,2,1,3,1,1,21002945,5,4,4,5,7,10,3,2,1,21015425,3,1,1,1,2,2,3,1,1,2......
我不知道哪里出了问题。请帮帮我。谢谢!
回答:
我通过删除 txt 文件中的所有空格修复了错误。尝试使用 id,clump_thick,unif_cell_size,unif_cell_shape,marg_adhesion,sing_epith_cell_size,bare_nuclei,bland_chromatin,normal_nucl,mitoses,class(没有空格)
希望对你有帮助!