IndexError:索引过多 [英] IndexError: too many indices
本文介绍了IndexError:索引过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试在scikit-learn中使用一种算法,根据许多输入来预测输出.我似乎正在得到错误索引过多",但无法弄清楚原因.
I am trying to use an algorithm in scikit-learn to predict the output based on a number of inputs. I seem to be getting the error 'too many indices' returned, but cannot figure out why.
CSV文件培训:
1.1 0.2 0.1 0 0.12 0.1
1.4 0.2 0.1 0.1 0.14 0.1
0.1 0.1 0.1 0 0.26 0.1
24.5 0.1 0 0.1 0.14 0.1
0.1 0.1 0.1 0 0.25 0.1
代码:
fileCSVTraining = genfromtxt('TrainingData.csv', delimiter=',', dtype=None)
#Define first 6 rows of data as the features
t = fileCSVTraining[:, 6:]
#Define which column to put prediction in
r = fileCSVTraining[:, 0-6:]
#Create and train classifier
x, y = r, t
clf = LinearSVC()
clf = clf.fit(x, y)
#New data to predict
X_new = [1.0, 2.1, 3.0, 2.4, 2.1]
b = clf.predict(X_new)
错误:
t = fileCSVTraining[:, 6:]
IndexError: too many indices
推荐答案
根据评论,我认为您想要:
Based on the comments, I think you want:
fileCSVTraining = genfromtxt('TrainingData.csv')
然后,要获取前6行",您将使用
Then, to get the "first 6 rows", you would use
t = fileCSVTraining[:6, :]
(我假设您的实际数据文件比您显示的长.您的示例只有5行.)
(I'm assuming your actual data file is longer than you've shown. Your example has only 5 rows.)
我怀疑您使用数组索引获取r
也是错误的.
I suspect your use of array indexing to get r
is also incorrect.
这篇关于IndexError:索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文