Python / Sklearn - IndexError - 索引超出范围

时间:2017-11-15 14:00:29

标签: python pandas scikit-learn

我正在尝试使用10倍CV在我的数据集中运行kNN分类器。我对WEKA的模型有一些经验,但很难将其转移到Sklearn。

以下是我的代码

filename = 'train4.csv'
names = ['attribut names are here']
dataframe = read_csv(filename, names=names)
array = dataframe.values
X = array[:,0:47]
Y = array[:,47]
num_folds = 10
kfold = KFold(n_splits=10, random_state=7)
model = KNeighborsClassifier()
results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())

我收到错误

>IndexError                                Traceback (most recent call last)
<ipython-input-19-8d9596c3368b> in <module>()
      4 array = dataframe.values
      5 X = array[:,0:47]
----> 6 Y = array[:,47]
      7 num_folds = 10
      8 kfold = KFold(n_splits=10, random_state=7)

> IndexError: index 47 is out of bounds for axis 1 with size 47

在我的CSV中,第47个属性是目标标签 - 因此是48(我错了吗?)。

我在Jupyter笔记本中运行pandas / sklearn。

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

import pandas as pd

filename = 'train4.csv'
names = ['attribut names are here']
target_col_name = 'name_of_your_target_column'

df = pd.read_csv(filename, names=names)

num_folds = 10
kfold = KFold(n_splits=10, random_state=7)
model = KNeighborsClassifier()
results = cross_val_score(model,
                          df.drop(target_col_name, axis=1), 
                          df[target_col_name],
                          cv=kfold)
print(results.mean())