更新时间:2022-03-03 09:25:17
您可以按降序对概率进行排序并检索前 n 个.要计算准确度,您可以定义自定义函数,如果您的 y_true
属于 top-n,则该函数将认为预测是正确的.沿着这些路线的东西应该可以工作:
You can sort your probabilities in descending order and retrieve the top-n. To calculate accuracy, you can define your custom function that will consider a prediction to be correct if your y_true
belongs in top-n. Something along these lines should work:
probs = clf.predict_proba(X_test)
# Sort desc and only extract the top-n
top_n = np.argsort(probs)[:,:-n-1:-1]
# Calculate accuracy
true_preds = 0
for i in range(len(y_test)):
if y_test[i] in top_n[i]:
true_preds += 1
accuracy = true_preds/len(y_test)