def compute_precision_and_recall(confusion_matrix):
correct_predictions = np.diagonal(confusion_matrix)
samples_per_class = np.sum(confusion_matrix, axis=0)
false_positives = np.sum(confusion_matrix, axis=1) - correct_predictions
false_negatives = samples_per_class - correct_predictions
prectmp = correct_predictions / (correct_predictions + false_positives)
prectmp[np.where(correct_predictions == 0)[0]] = 0
prectmp[np.where(samples_per_class == 0)[0]] = float('nan')
precision = np.nanmean(prectmp)
rectmp = correct_predictions / (correct_predictions + false_negatives)
rectmp[np.where(correct_predictions == 0)[0]] = 0
rectmp[np.where(samples_per_class == 0)[0]] = float('nan')
recall = np.nanmean(rectmp)
return precision, recall
评论列表
文章目录