无法索引

时间:2018-06-26 21:02:49

标签: ipython jupyter-notebook

运行下一部分代码时,出现以下错误:

for record in training_data_list:
        all_values = record.split(',')
        y_inputs = (np.asfarray(all_values[0]))

        Max = np.zeros(10)
        Max[int(all_values[0])] = 1

        A = A[Max[int(all_values[0])]]

错误是:

 # print("index=" + str(Max[int(all_values[0])])

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

运行代码时:

for record in training_data_list:
        all_values = record.split(',')
        y_inputs = (np.asfarray(all_values[0]))

        Max = np.zeros(10)
        Max[int(all_values[0])] = 1

        A = A[int(Max[int(all_values[0])])]

错误是:

---> 22         A = A[int(Max[int(all_values[0])])]
     23 
     24        # print("index=" + str(Max[int(all_values[0])])

IndexError: invalid index to scalar variable.

有人知道如何使用Max[int(all_values[0])]作为索引吗?

A的形状是(10,103)

在编写代码时,例如:

for j in range (0, 9):
    print("A index j =" + str(A[j]))

打印我的索引A的值。那么为什么它不能像上面的代码那样工作?

1 个答案:

答案 0 :(得分:0)

问题是int(all_values[0])不是有效的索引,或者Max[int(all_values[0])]不是有效的索引。打印这两个以查看您所引用的是什么。