gpt4 book ai didi

Python/Sklearn - IndexError - 索引越界

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:56 26 4
gpt4 key购买 nike

我正在尝试使用 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。

谢谢

最佳答案

试试这个:

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())

关于Python/Sklearn - IndexError - 索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47309376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com