K Nearest Neighbors 首次应用。Pandas 中 drop 函数的错误

我尝试使用 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(没有空格)

希望对你有帮助!

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注