python 多分类 recall_python + sklearn ︱分类效果评估——acc、recall、F1、ROC、回归、距离...

.

一、acc、recall、F1、混淆矩阵、分类综合报告

1、准确率

第一种方式:accuracy_score

# 准确率

import numpy as np

from sklearn.metrics import accuracy_score

y_pred = [0, 2, 1, 3,9,9,8,5,8]

y_true = [0, 1, 2, 3,2,6,3,5,9]

accuracy_score(y_true, y_pred)

Out[127]: 0.33333333333333331

accuracy_score(y_true, y_pred, normalize=False) # 类似海明距离,每个类别求准确后,再求微平均

Out[128]: 3

第二种方式:metrics

宏平均比微平均更合理,但也不是说微平均一无是处,具体使用哪种评测机制,还是要取决于数据集中样本分布

宏平均(Macro-averaging),是先对每一个类统计指标值,然后在对所有类求算术平均值。

下面是一个使用Python实现Accuracy类、F1度量类、P-R曲线类、ROC曲线类和AUC类的示例代码: ```python import matplotlib.pyplot as plt class Accuracy: def __init__(self, y_true, y_pred): self.y_true = y_true self.y_pred = y_pred def accuracy_score(self): correct = sum([1 for yt, yp in zip(self.y_true, self.y_pred) if yt == yp]) total = len(self.y_true) accuracy = correct / total return accuracy class F1Score: def __init__(self, y_true, y_pred): self.y_true = y_true self.y_pred = y_pred def precision_recall_f1(self): true_positives = sum([1 for yt, yp in zip(self.y_true, self.y_pred) if yt == 1 and yp == 1]) false_positives = sum([1 for yt, yp in zip(self.y_true, self.y_pred) if yt == 0 and yp == 1]) false_negatives = sum([1 for yt, yp in zip(self.y_true, self.y_pred) if yt == 1 and yp == 0]) precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0 recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0 f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0 return precision, recall, f1_score class PRCurve: def __init__(self, y_true, y_scores): self.y_true = y_true self.y_scores = y_scores def precision_recall_curve(self): thresholds = sorted(set(self.y_scores), reverse=True) precisions = [] recalls = [] for threshold in thresholds: y_pred = [1 if score >= threshold else 0 for score in self.y_scores] true_positives = sum([1 for yt, yp in zip(self.y_true, y_pred) if yt == 1 and yp == 1]) false_positives = sum([1 for yt, yp in zip(self.y_true, y_pred) if yt == 0 and yp == 1]) false_negatives = sum([1 for yt, yp in zip(self.y_true, y_pred) if yt == 1 and yp == 0]) precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0 recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0 precisions.append(precision) recalls.append(recall) return precisions, recalls class ROCCurve: def __init__(self, y_true, y_scores): self.y_true = y_true self.y_scores = y_scores def roc_curve(self): thresholds = sorted(set(self.y_scores), reverse=True) tpr_values = [] fpr_values = [] num_positive_cases = sum([1 for yt in self.y_true if yt == 1]) num_negative_cases = sum([1 for yt in self.y_true if yt == 0]) for threshold in thresholds: y_pred = [1 if score >= threshold else 0 for score in self.y_scores] true_positives = sum([1 for yt, yp in zip(self.y_true, y_pred) if yt == 1 and yp == 1]) false_positives = sum([1 for yt, yp in zip(self.y_true, y_pred) if yt == 0 and yp == 1]) tpr = true_positives / num_positive_cases if num_positive_cases > 0 else 0 fpr = false_positives / num_negative_cases if num_negative_cases > 0 else 0 tpr_values.append(tpr) fpr_values.append(fpr) return tpr_values, fpr_values class AUC: def __init__(self, tpr, fpr): self.tpr = tpr self.fpr = fpr def auc_score(self): auc = 0 for i in range(1, len(self.fpr)): auc += (self.fpr[i] - self.fpr[i-1]) * (self.tpr[i] + self.tpr[i-1]) / 2 return auc # 示例数据 y_true = [1, 0, 1, 1, 0, 0, 1] y_scores = [0.9, 0.6, 0.8, 0.7, 0.4, 0.3, 0.5] # 计算并输出准确率 accuracy = Accuracy(y_true, y_scores) acc = accuracy.accuracy_score() print("Accuracy:", acc) # 计算并输出精确率、召回率和F1度量 f1_score = F1Score(y_true, y_scores) precision, recall, f1 = f1_score.precision_recall_f1() print("Precision:", precision) print("Recall:", recall) print("F1 Score:", f1) # 计算并绘制P-R曲线 pr_curve = PRCurve(y_true, y_scores) precisions, recalls = pr_curve.precision_recall_curve() plt.plot(recalls, precisions) plt.xlabel('Recall') plt.ylabel('Precision') plt.title('P-R Curve') plt.show() # 计算并绘制ROC曲线 roc_curve = ROCCurve(y_true, y_scores) tpr_values, fpr_values = roc_curve.roc_curve() plt.plot(fpr_values, tpr_values) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('ROC Curve') plt.show() # 计算并输出AUC auc = AUC(tpr_values, fpr_values) auc_score = auc.auc_score() print("AUC Score:", auc_score) ``` 这段代码展示了如何实现Accuracy类、F1度量类、P-R曲线类、ROC曲线类和AUC类。你可以根据你的实际需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值