python - 索引错误 : too many indices

标签 python numpy scikit-learn

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

相关文章:

python - 如何使用 numpy python 更改 SVD 的顺序

python - 在Python中将元组元素切片并堆叠到矩阵中

python - 如何将多个分布列表传递给sklearn randomizedSearchCV

python - svm.sparse.SVC 需要花费大量时间进行训练

python - 删除 NumPy 数组中重复的行

python - SPI_SETSCREENSAVETIMEOUT 不适用于使用 Python 和 ctypes 的 Windows 7

python - 只是对 NumPy 函数的结果感到好奇!

python - Pandas .agg 函数有哪些?

python - 如何查找方括号中的文本

python - 属性错误 : 'RandomForestClassifier' object has no attribute 'fit_transform'