Spaces:
Sleeping
Sleeping
File size: 14,195 Bytes
04103fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
"""
评估指标计算工具
"""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import (
accuracy_score, precision_recall_fscore_support,
confusion_matrix, classification_report, cohen_kappa_score,
roc_auc_score, roc_curve, auc
)
from sklearn.preprocessing import label_binarize
import pandas as pd
from typing import List, Dict, Tuple, Optional
import warnings
warnings.filterwarnings('ignore')
def calculate_metrics(y_true: List[int], y_pred: List[int],
class_names: List[str] = None) -> Dict:
"""
计算分类指标
Args:
y_true: 真实标签
y_pred: 预测标签
class_names: 类别名称
Returns:
Dict: 指标字典
"""
# 基本指标
accuracy = accuracy_score(y_true, y_pred)
precision, recall, f1, support = precision_recall_fscore_support(
y_true, y_pred, average=None, zero_division=0
)
# 宏观和微观平均
macro_precision, macro_recall, macro_f1, _ = precision_recall_fscore_support(
y_true, y_pred, average='macro', zero_division=0
)
micro_precision, micro_recall, micro_f1, _ = precision_recall_fscore_support(
y_true, y_pred, average='micro', zero_division=0
)
weighted_precision, weighted_recall, weighted_f1, _ = precision_recall_fscore_support(
y_true, y_pred, average='weighted', zero_division=0
)
# Cohen's Kappa
kappa = cohen_kappa_score(y_true, y_pred)
# 混淆矩阵
cm = confusion_matrix(y_true, y_pred)
metrics = {
'accuracy': accuracy,
'macro_precision': macro_precision,
'macro_recall': macro_recall,
'macro_f1': macro_f1,
'micro_precision': micro_precision,
'micro_recall': micro_recall,
'micro_f1': micro_f1,
'weighted_precision': weighted_precision,
'weighted_recall': weighted_recall,
'weighted_f1': weighted_f1,
'cohen_kappa': kappa,
'confusion_matrix': cm,
'support': support
}
# 每个类别的指标
if class_names is None:
class_names = [f'Class_{i}' for i in range(len(precision))]
for i, class_name in enumerate(class_names):
if i < len(precision):
metrics[f'{class_name}_precision'] = precision[i]
metrics[f'{class_name}_recall'] = recall[i]
metrics[f'{class_name}_f1'] = f1[i]
metrics[f'{class_name}_support'] = support[i]
return metrics
def calculate_multiclass_auc(y_true: np.ndarray, y_scores: np.ndarray,
num_classes: int) -> Dict:
"""
计算多类别AUC指标
Args:
y_true: 真实标签 (one-hot或标签编码)
y_scores: 预测概率
num_classes: 类别数量
Returns:
Dict: AUC指标
"""
# 转换为one-hot编码
if y_true.ndim == 1:
y_true_binary = label_binarize(y_true, classes=range(num_classes))
if num_classes == 2:
y_true_binary = np.hstack([1-y_true_binary, y_true_binary])
else:
y_true_binary = y_true
# 计算每个类别的AUC
auc_scores = {}
fpr = {}
tpr = {}
for i in range(num_classes):
if np.sum(y_true_binary[:, i]) > 0: # 确保类别存在
fpr[i], tpr[i], _ = roc_curve(y_true_binary[:, i], y_scores[:, i])
auc_scores[f'class_{i}_auc'] = auc(fpr[i], tpr[i])
# 宏观平均AUC
if len(auc_scores) > 0:
auc_scores['macro_auc'] = np.mean(list(auc_scores.values()))
# 微观平均AUC(多类别)
try:
micro_auc = roc_auc_score(y_true_binary, y_scores, average='micro', multi_class='ovr')
auc_scores['micro_auc'] = micro_auc
except:
pass
auc_scores['fpr'] = fpr
auc_scores['tpr'] = tpr
return auc_scores
def plot_confusion_matrix(cm: np.ndarray, class_names: List[str],
normalize: bool = False, title: str = 'Confusion Matrix',
save_path: str = None, figsize: Tuple[int, int] = (10, 8)):
"""
绘制混淆矩阵
Args:
cm: 混淆矩阵
class_names: 类别名称
normalize: 是否标准化
title: 图标题
save_path: 保存路径
figsize: 图像大小
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fmt = '.2f'
else:
fmt = 'd'
plt.figure(figsize=figsize)
sns.heatmap(cm, annot=True, fmt=fmt, cmap='Blues',
xticklabels=class_names, yticklabels=class_names,
cbar_kws={'label': 'Count' if not normalize else 'Proportion'})
plt.title(title, fontsize=16)
plt.xlabel('Predicted Label', fontsize=14)
plt.ylabel('True Label', fontsize=14)
plt.xticks(rotation=45)
plt.yticks(rotation=0)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"混淆矩阵已保存: {save_path}")
return plt.gcf()
def plot_roc_curves(fpr: Dict, tpr: Dict, auc_scores: Dict,
class_names: List[str], save_path: str = None,
figsize: Tuple[int, int] = (12, 8)):
"""
绘制ROC曲线
Args:
fpr: 假正率字典
tpr: 真正率字典
auc_scores: AUC分数字典
class_names: 类别名称
save_path: 保存路径
figsize: 图像大小
"""
plt.figure(figsize=figsize)
# 绘制每个类别的ROC曲线
colors = plt.cm.Set1(np.linspace(0, 1, len(class_names)))
for i, (color, class_name) in enumerate(zip(colors, class_names)):
if i in fpr and i in tpr:
auc_score = auc_scores.get(f'class_{i}_auc', 0)
plt.plot(fpr[i], tpr[i], color=color, lw=2,
label=f'{class_name} (AUC = {auc_score:.3f})')
# 对角线
plt.plot([0, 1], [0, 1], 'k--', lw=2, alpha=0.8)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', fontsize=14)
plt.ylabel('True Positive Rate', fontsize=14)
plt.title('Receiver Operating Characteristic (ROC) Curves', fontsize=16)
plt.legend(loc="lower right")
plt.grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"ROC曲线已保存: {save_path}")
return plt.gcf()
def plot_precision_recall_curve(y_true: np.ndarray, y_scores: np.ndarray,
class_names: List[str], save_path: str = None,
figsize: Tuple[int, int] = (12, 8)):
"""
绘制Precision-Recall曲线
Args:
y_true: 真实标签 (one-hot编码)
y_scores: 预测概率
class_names: 类别名称
save_path: 保存路径
figsize: 图像大小
"""
from sklearn.metrics import precision_recall_curve, average_precision_score
plt.figure(figsize=figsize)
colors = plt.cm.Set1(np.linspace(0, 1, len(class_names)))
for i, (color, class_name) in enumerate(zip(colors, class_names)):
if i < y_scores.shape[1]:
precision, recall, _ = precision_recall_curve(y_true[:, i], y_scores[:, i])
ap_score = average_precision_score(y_true[:, i], y_scores[:, i])
plt.plot(recall, precision, color=color, lw=2,
label=f'{class_name} (AP = {ap_score:.3f})')
plt.xlabel('Recall', fontsize=14)
plt.ylabel('Precision', fontsize=14)
plt.title('Precision-Recall Curves', fontsize=16)
plt.legend(loc="lower left")
plt.grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Precision-Recall曲线已保存: {save_path}")
return plt.gcf()
def create_classification_report(y_true: List[int], y_pred: List[int],
class_names: List[str] = None,
save_path: str = None) -> pd.DataFrame:
"""
创建分类报告DataFrame
Args:
y_true: 真实标签
y_pred: 预测标签
class_names: 类别名称
save_path: 保存路径
Returns:
pd.DataFrame: 分类报告
"""
report_dict = classification_report(
y_true, y_pred,
target_names=class_names,
output_dict=True,
zero_division=0
)
df_report = pd.DataFrame(report_dict).transpose()
if save_path:
df_report.to_csv(save_path, encoding='utf-8')
print(f"分类报告已保存: {save_path}")
return df_report
def evaluate_model_comprehensive(y_true: List[int], y_pred: List[int],
y_scores: Optional[np.ndarray] = None,
class_names: List[str] = None,
output_dir: str = 'evaluation_results') -> Dict:
"""
综合评估模型
Args:
y_true: 真实标签
y_pred: 预测标签
y_scores: 预测概率(用于AUC计算)
class_names: 类别名称
output_dir: 输出目录
Returns:
Dict: 评估结果
"""
import os
os.makedirs(output_dir, exist_ok=True)
# 基础指标
metrics = calculate_metrics(y_true, y_pred, class_names)
# 混淆矩阵可视化
cm = metrics['confusion_matrix']
# 原始计数
plot_confusion_matrix(
cm, class_names, normalize=False,
title='Confusion Matrix (Counts)',
save_path=os.path.join(output_dir, 'confusion_matrix_counts.png')
)
plt.close()
# 标准化
plot_confusion_matrix(
cm, class_names, normalize=True,
title='Confusion Matrix (Normalized)',
save_path=os.path.join(output_dir, 'confusion_matrix_normalized.png')
)
plt.close()
# 分类报告
report_df = create_classification_report(
y_true, y_pred, class_names,
save_path=os.path.join(output_dir, 'classification_report.csv')
)
# AUC相关指标(如果提供了概率)
if y_scores is not None:
num_classes = len(class_names) if class_names else len(np.unique(y_true))
# 转换标签为one-hot编码
y_true_binary = label_binarize(y_true, classes=range(num_classes))
if num_classes == 2:
y_true_binary = np.hstack([1-y_true_binary, y_true_binary])
# 计算AUC
auc_metrics = calculate_multiclass_auc(y_true_binary, y_scores, num_classes)
metrics.update(auc_metrics)
# ROC曲线
plot_roc_curves(
auc_metrics['fpr'], auc_metrics['tpr'], auc_metrics,
class_names, save_path=os.path.join(output_dir, 'roc_curves.png')
)
plt.close()
# Precision-Recall曲线
plot_precision_recall_curve(
y_true_binary, y_scores, class_names,
save_path=os.path.join(output_dir, 'precision_recall_curves.png')
)
plt.close()
# 保存指标到文件
metrics_df = pd.DataFrame([
{'metric': k, 'value': v} for k, v in metrics.items()
if isinstance(v, (int, float, np.number))
])
metrics_df.to_csv(os.path.join(output_dir, 'metrics_summary.csv'), index=False)
print(f"评估结果已保存到目录: {output_dir}")
return metrics
def print_metrics_summary(metrics: Dict, class_names: List[str] = None):
"""
打印指标摘要
Args:
metrics: 指标字典
class_names: 类别名称
"""
print("=" * 60)
print("模型评估结果摘要")
print("=" * 60)
print(f"总体准确率: {metrics['accuracy']:.4f}")
print(f"Cohen's Kappa: {metrics['cohen_kappa']:.4f}")
print()
print("宏观平均:")
print(f" 精确率: {metrics['macro_precision']:.4f}")
print(f" 召回率: {metrics['macro_recall']:.4f}")
print(f" F1分数: {metrics['macro_f1']:.4f}")
print()
print("加权平均:")
print(f" 精确率: {metrics['weighted_precision']:.4f}")
print(f" 召回率: {metrics['weighted_recall']:.4f}")
print(f" F1分数: {metrics['weighted_f1']:.4f}")
print()
if class_names:
print("各类别性能:")
print("-" * 60)
print(f"{'类别':<15} {'精确率':<10} {'召回率':<10} {'F1分数':<10} {'样本数':<10}")
print("-" * 60)
for i, class_name in enumerate(class_names):
precision_key = f'{class_name}_precision'
recall_key = f'{class_name}_recall'
f1_key = f'{class_name}_f1'
support_key = f'{class_name}_support'
if all(key in metrics for key in [precision_key, recall_key, f1_key, support_key]):
print(f"{class_name:<15} "
f"{metrics[precision_key]:<10.4f} "
f"{metrics[recall_key]:<10.4f} "
f"{metrics[f1_key]:<10.4f} "
f"{int(metrics[support_key]):<10}")
if 'macro_auc' in metrics:
print(f"\n宏观平均AUC: {metrics['macro_auc']:.4f}")
if 'micro_auc' in metrics:
print(f"微观平均AUC: {metrics['micro_auc']:.4f}")
print("=" * 60)
if __name__ == "__main__":
# 测试评估指标
np.random.seed(42)
# 生成模拟数据
n_samples = 1000
n_classes = 5
y_true = np.random.randint(0, n_classes, n_samples)
y_pred = np.random.randint(0, n_classes, n_samples)
y_scores = np.random.rand(n_samples, n_classes)
y_scores = y_scores / y_scores.sum(axis=1, keepdims=True) # 归一化为概率
class_names = ['无病变', '轻度', '中度', '重度', '增殖性']
# 综合评估
results = evaluate_model_comprehensive(
y_true, y_pred, y_scores, class_names, 'test_evaluation'
)
# 打印摘要
print_metrics_summary(results, class_names)
|