那年夏天我和你躲在 这一大片宁静的海
直到后来我们都还在 对这个世界充满期待
今年冬天你已经不在 我的心空出了一块
很高兴遇见你 让我终究明白
回忆比真实精彩
🎵 王心凌《那年夏天宁静的海》
from sklearn.metrics import confusion_matrix
def calculate_metrics(y_true, y_pred):
# 计算混淆矩阵
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
# 计算各个指标
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp) if (tp + fp) != 0 else 0
recall = tp / (tp + fn) if (tp + fn) != 0 else 0
f1_score = (2 * precision * recall) / (precision + recall) if (precision + recall) != 0 else 0
specificity = tn / (tn + fp) if (tn + fp) != 0 else 0
return {
'True Positive (TP)': tp,
'False Positive (FP)': fp,
'True Negative (TN)': tn,
'False Negative (FN)': fn,
'Accuracy': accuracy,
'Precision': precision,
'Recall': recall,
'F1 Score': f1_score,
'Specificity': specificity
}
# 示例用法
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0] # 真实标签
y_pred = [1, 0, 1, 0, 0, 1, 0, 1, 1, 0] # 预测标签
metrics = calculate_metrics(y_true, y_pred)
for metric, value in metrics.items():
print(f'{metric}: {value}')