首页 > 分享 > “None of [Int64Index([0, 1, 2, 3], dtype=‘int64‘)] are in the [columns]错误探索

“None of [Int64Index([0, 1, 2, 3], dtype=‘int64‘)] are in the [columns]错误探索

用支持向量机做鸢尾花分类预测时
敲代码有个关于KeyError的报错,如下:
KeyError: “None of [Int64Index([0, 1, 2, 3], dtype=‘int64’)] are in the [columns]”
原始代码如下

import pandas as pd from sklearn.model_selection import train_test_split from sklearn import svm from sklearn.metrics import accuracy_score import numpy as np data = pd.read_csv(r"G:实验6/iris.csv") x, y = data[range(4)], data[4] y = pd.Categorical(y).codes x = x[[0,1,2,3]] x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, train_size=0.6) clf=svm.SVC(C=0.4,kernel='rbf',gamma=20,decision_function_shape='ovr') clf.fit(x_train, y_train.ravel()) print('训练集准确率:', accuracy_score(y_train, clf.predict(x_train))) print('测试集准确率:', accuracy_score(y_test, clf.predict(x_test))) 123456789101112131415

一开始看了很多博文
以为是数据类型的错误,或者是pandas包版本的问题,跟着他们的博文改,发现还是报错。
然后探索发现并不是,查看鸢尾花数据发现
在这里插入图片描述
鸢尾花数据集中没有索引行,详解见如下链接
https://www.cnblogs.com/komean/p/10629311.html

修改后代码不再报错
修改后代码如下:

import pandas as pd from sklearn.model_selection import train_test_split from sklearn import svm from sklearn.metrics import accuracy_score import numpy as np data = pd.read_csv(r"G:实验6/iris.csv",header=None) x, y = data[range(4)], data[4] y = pd.Categorical(y).codes x = x[[0,1,2,3]] x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, train_size=0.6) clf=svm.SVC(C=0.4,kernel='rbf',gamma=20,decision_function_shape='ovr') clf.fit(x_train, y_train.ravel()) print('训练集准确率:', accuracy_score(y_train, clf.predict(x_train))) print('测试集准确率:', accuracy_score(y_test, clf.predict(x_test))) 1234567891011121314

不再报错,运行成功
在这里插入图片描述

相关知识

【机器学习】鸢尾花分类:机器学习领域经典入门项目实战
做一个logitic分类之鸢尾花数据集的分类
TensorFlow入门
TensorFlow机器学习实战指南——山鸢尾花分类
基于TensorFlow实现的CNN神经网络 花卉识别系统Demo
使用迁移学习对花卉进行分类
【探索与发现,6分之1=2分之一+3分之112分之1=3分之1
设5阶行列式 1 1 0 0 0 1 2 0 0 0 D=0x2+x3 x3+x
神经网络
CropNet: Cassava Disease Detection

网址: “None of [Int64Index([0, 1, 2, 3], dtype=‘int64‘)] are in the [columns]错误探索 https://m.huajiangbk.com/newsview387284.html

所属分类:花卉
上一篇: KNN算法分类算法
下一篇: tensorflow