我正在尝试使用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 - IndexError:索引过多,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14793585/