[英]IndexError: too many indices
我正在尝试在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
也不正确。
请打印您的x
和y
变量,您很可能会看到数据无效的原因,并进行相应的修复。
同样对于最后一行:
X_new = [1.0, 2.1, 3.0, 2.4, 2.1]
b = clf.predict(X_new)
应该:
X_new = [[1.0, 2.1, 3.0, 2.4, 2.1]]
b = clf.predict(X_new)
如预期的那样,期望的是样本集合( (n_new_samples, n_features)
2D数组),而不是单个样本。
获取r和t的数组索引不正确。 使用:
t = fileCSVTraining[:, 1-0:]
得到了所需的训练数据,离开了预测列。
指定dtype = float也很重要,因为“无”将允许将整数(如果您的数据中有)包含在数组中,这将强制使用一维数组而不是二维数组。 如图所示,索引在1-D上不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.