索引1超出了0的轴,在python中大小为1

时间:2018-03-19 12:49:15

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

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

评估培训

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 什么是可能的解决方案。提前谢谢

1 个答案:

答案 0 :(得分:0)

这是抛出错误的部分

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

预测只包含一个值,即pred [i],但是您正在尝试索引预测[1],这超出了界限。

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

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