【问题标题】:index 1 is out of bounds for axis 0 with size 1 in python索引1超出了python中大小为1的轴0的范围
【发布时间】:2018-08-28 00:56:39
【问题描述】:

我正在尝试评估我的火车数据集:

评估培训

score = classifier.evaluate(X_test, y_test, verbose=1)
print("\nTest Results for {} test entries \
on which we did not trained the neural network.\n".format(len(X_test)))

print("Keras evaluation result:", score[0])
print("Percentage right: {}%.".format(score[1]*100))
print("Error: {}%.\n".format((1-score[1])*100))

def evaluate_model(classifier, X_test, y_test):
    confusion_matrix = np.array([
        [0, 0], 
        [0, 0]
    ])
    pred = classifier.predict(X_train)
    for i in range(len(pred)):
        prediction = pred[i]
        if prediction[0]>prediction[1]:
            prediction = 1
        else:
            prediction = 0

        expected = y_train[i][0]
        confusion_matrix[prediction][expected] += 1

    return confusion_matrix

confusion_matrix = evaluate_model(classifier, X_test, y_test)
confusion_matrix_interpretation = np.array([
        ["true negative", "false negative"], 
        ["false positive", "true positive"]
    ])
print("Confusion matrix:")
print(confusion_matrix)
print("Confusion matrix, percentage of data:")
print(confusion_matrix*100/sum(confusion_matrix.flatten()))
print("Confusion matrix interpretation:\n", confusion_matrix_interpretation)

问题:对于大小为 1 的轴 0,索引 1 超出范围 可能的解决方案是什么。提前致谢

【问题讨论】:

  • IndexError: 索引 1 超出轴 0 的范围,大小为 1
  • 你能分享你得到的错误的整个追溯吗?

标签: python tensorflow machine-learning keras artificial-intelligence


【解决方案1】:

这是引发错误的部分

prediction = pred[i]
if prediction[0]>prediction[1]:

prediction 仅包含一个值,即 pred[i],但您正在尝试索引 prediction[1],这是超出范围的。

您似乎正在尝试查找具有最大概率的标签。在这种情况下使用

prediction=np.argmax(pred,axis=1)

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2015-10-13
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多