我正在尝试使用 scikit-learn 中的算法根据大量输入来预测输出。我似乎收到错误“索引过多”,但无法弄清楚原因。
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
最佳答案
根据评论,我认为您想要:
fileCSVTraining = genfromtxt('TrainingData.csv')
然后,要获得“前 6 行”,您可以使用
t = fileCSVTraining[:6, :]
(我假设您的实际数据文件比您显示的要长。您的示例只有 5 行。)
我怀疑您使用数组索引来获取 r
也是不正确的。
关于python - 索引错误 : too many indices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793585/