python实现Accuracy类 F1度量类 P-R曲线类 ROC曲线类 AUC类 ,不用sklearn
时间: 2024-03-29 16:07:33 浏览: 137
python实现二分类和多分类的ROC曲线教程
5星 · 资源好评率100%
下面是一个使用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类。你可以根据你的实际需求进行修改和优化。
阅读全文