-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep3_algorithm_comparison.py
More file actions
721 lines (585 loc) · 26.5 KB
/
Copy pathstep3_algorithm_comparison.py
File metadata and controls
721 lines (585 loc) · 26.5 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# -*- coding: utf-8 -*-
"""
水质安全预测项目 - 步骤3: 比较不同算法性能
目的: 学习如何系统地比较多种机器学习算法,选择最适合的模型
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score, roc_curve
from sklearn.model_selection import cross_val_score
import joblib
import time
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def load_training_data():
"""
加载训练数据和已训练的神经网络模型
从前面步骤中获取数据和模型
"""
print("=" * 60)
print("步骤3: 加载数据和已训练模型")
print("=" * 60)
try:
# 加载预处理数据
X_tensor = torch.load("preprocessed_data/X_tensor.pt", weights_only=True)
y_tensor = torch.load("preprocessed_data/y_tensor.pt", weights_only=True)
# 转换为numpy格式 (传统机器学习算法需要)
X = X_tensor.numpy()
y = y_tensor.numpy()
# 加载已训练的神经网络 - 使用安全加载方式
try:
# 首先尝试安全加载模型状态
# 需要先导入模型类定义
import sys
sys.path.append('.')
# 重新定义模型类 (与step2中的定义相同)
class WaterQualityClassifier(nn.Module):
def __init__(self, input_size, hidden_size=64, dropout_rate=0.3):
super(WaterQualityClassifier, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.dropout1 = nn.Dropout(dropout_rate)
self.fc2 = nn.Linear(hidden_size, hidden_size // 2)
self.dropout2 = nn.Dropout(dropout_rate)
self.fc3 = nn.Linear(hidden_size // 2, 2)
self.relu = nn.ReLU()
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.dropout1(x)
x = self.fc2(x)
x = self.relu(x)
x = self.dropout2(x)
x = self.fc3(x)
return x
# 创建模型实例并加载权重
input_size = X.shape[1]
nn_model = WaterQualityClassifier(input_size=input_size)
model_state = torch.load("trained_model/model_state.pt", weights_only=True)
nn_model.load_state_dict(model_state)
nn_model.eval()
print("✓ 使用模型状态文件成功加载神经网络")
except Exception as model_load_error:
print(f"⚠️ 模型状态加载失败,尝试完整模型加载: {model_load_error}")
# 如果模型状态加载失败,尝试加载完整模型 (不推荐,但兼容性考虑)
try:
nn_model = torch.load("trained_model/complete_model.pt", weights_only=False)
print("✓ 使用完整模型文件成功加载神经网络 (安全性较低)")
except Exception as complete_model_error:
print(f"❌ 完整模型加载也失败: {complete_model_error}")
raise complete_model_error
# 加载评估指标 - 处理numpy兼容性
try:
# 首先尝试安全加载
nn_metrics = torch.load("trained_model/evaluation_metrics.pt", weights_only=True)
except Exception as metrics_error:
print(f"⚠️ 安全模式加载指标失败,使用兼容模式: {str(metrics_error)[:100]}...")
# 使用兼容模式加载(包含numpy数组的文件)
nn_metrics = torch.load("trained_model/evaluation_metrics.pt", weights_only=False)
print("✓ 使用兼容模式成功加载评估指标")
print(f"✓ 成功加载数据和模型")
print(f"数据形状: {X.shape}")
print(f"已训练神经网络准确率: {nn_metrics['accuracy']:.4f}")
return X, y, nn_model, nn_metrics
except Exception as e:
print(f"❌ 数据加载失败: {e}")
print("请确保已完成步骤1和步骤2")
print("\n🔧 解决方案:")
print("1. 确认 preprocessed_data/ 和 trained_model/ 目录存在")
print("2. 确认已成功运行 step1_preprocessing.py 和 step2_classification.py")
print("3. 如果使用PyTorch 2.6+,这是正常的安全检查")
return None, None, None, None
def split_data_for_comparison(X, y, test_size=0.2, random_state=42):
"""
为算法比较划分数据
使用与神经网络相同的划分方式确保公平比较
"""
from sklearn.model_selection import train_test_split
print(f"\n数据划分 (用于算法比较):")
print(f"训练集: {(1 - test_size) * 100:.0f}%, 测试集: {test_size * 100:.0f}%")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state, stratify=y
)
print(f"训练集大小: {X_train.shape[0]}")
print(f"测试集大小: {X_test.shape[0]}")
return X_train, X_test, y_train, y_test
def create_traditional_models():
"""
创建传统机器学习模型
学习不同类型算法的特点和适用场景
"""
print("\n" + "=" * 60)
print("步骤3.1: 创建传统机器学习模型")
print("=" * 60)
models = {
'Logistic Regression': {
'model': LogisticRegression(random_state=42, max_iter=1000),
'description': '线性分类器,简单快速,适合线性可分数据'
},
'Random Forest': {
'model': RandomForestClassifier(n_estimators=100, random_state=42),
'description': '集成学习,组合多个决策树,处理非线性关系强'
},
'Gradient Boosting': {
'model': GradientBoostingClassifier(n_estimators=100, random_state=42),
'description': '梯度提升,逐步改进模型,通常准确率很高'
},
'Support Vector Machine': {
'model': SVC(probability=True, random_state=42),
'description': '支持向量机,在高维空间中寻找最优分类边界'
}
}
print("将要比较的算法:")
for name, info in models.items():
print(f" • {name}: {info['description']}")
return models
def train_and_evaluate_traditional_models(models, X_train, X_test, y_train, y_test):
"""
训练和评估传统机器学习模型
学习如何系统地比较不同算法的性能
"""
print("\n" + "=" * 60)
print("步骤3.2: 训练传统机器学习模型")
print("=" * 60)
results = {}
for name, model_info in models.items():
print(f"\n正在训练: {name}")
print("-" * 40)
model = model_info['model']
# 记录训练时间
start_time = time.time()
# 训练模型
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1] if hasattr(model, 'predict_proba') else None
# 计算训练时间
training_time = time.time() - start_time
# 计算评估指标
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
auc = roc_auc_score(y_test, y_prob) if y_prob is not None else None
# 交叉验证评估
cv_scores = cross_val_score(model, X_train, y_train, cv=5)
# 存储结果
results[name] = {
'model': model,
'accuracy': accuracy,
'precision': precision,
'recall': recall,
'f1': f1,
'auc': auc,
'training_time': training_time,
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std(),
'predictions': y_pred,
'probabilities': y_prob
}
# 打印结果
print(f"准确率: {accuracy:.4f}")
print(f"精确率: {precision:.4f}")
print(f"召回率: {recall:.4f}")
print(f"F1分数: {f1:.4f}")
if auc:
print(f"AUC: {auc:.4f}")
print(f"训练时间: {training_time:.2f}秒")
print(f"5折交叉验证: {cv_scores.mean():.4f} ± {cv_scores.std():.4f}")
return results
def evaluate_neural_network(nn_model, X_test, y_test):
"""
评估神经网络模型
将神经网络的结果统一格式以便比较
"""
print(f"\n评估神经网络模型:")
print("-" * 40)
# 转换数据为PyTorch张量
X_test_tensor = torch.FloatTensor(X_test)
test_dataset = TensorDataset(X_test_tensor, torch.LongTensor(y_test))
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# 预测
nn_model.eval()
all_predictions = []
all_probabilities = []
with torch.no_grad():
for batch_X, batch_y in test_loader:
outputs = nn_model(batch_X)
probabilities = F.softmax(outputs, dim=1)
_, predicted = torch.max(outputs, 1)
all_predictions.extend(predicted.numpy())
all_probabilities.extend(probabilities[:, 1].numpy()) # 取正类概率
y_pred = np.array(all_predictions)
y_prob = np.array(all_probabilities)
# 计算指标
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
auc = roc_auc_score(y_test, y_prob)
print(f"神经网络性能:")
print(f"准确率: {accuracy:.4f}")
print(f"精确率: {precision:.4f}")
print(f"召回率: {recall:.4f}")
print(f"F1分数: {f1:.4f}")
print(f"AUC: {auc:.4f}")
return {
'accuracy': accuracy,
'precision': precision,
'recall': recall,
'f1': f1,
'auc': auc,
'predictions': y_pred,
'probabilities': y_prob
}
def create_comparison_table(traditional_results, nn_results):
"""
创建算法性能比较表
学习如何系统地比较和分析不同算法的性能
"""
print("\n" + "=" * 60)
print("步骤3.3: 算法性能比较分析")
print("=" * 60)
# 准备比较数据
comparison_data = []
# 添加传统机器学习算法结果
for name, results in traditional_results.items():
comparison_data.append({
'算法': name,
'准确率': results['accuracy'],
'精确率': results['precision'],
'召回率': results['recall'],
'F1分数': results['f1'],
'AUC': results['auc'] if results['auc'] else 0,
'训练时间(秒)': results['training_time'],
'交叉验证均值': results['cv_mean'],
'交叉验证标准差': results['cv_std']
})
# 添加神经网络结果
comparison_data.append({
'算法': 'Neural Network',
'准确率': nn_results['accuracy'],
'精确率': nn_results['precision'],
'召回率': nn_results['recall'],
'F1分数': nn_results['f1'],
'AUC': nn_results['auc'],
'训练时间(秒)': None, # 神经网络训练时间在步骤2中记录
'交叉验证均值': None,
'交叉验证标准差': None
})
# 创建DataFrame
comparison_df = pd.DataFrame(comparison_data)
# 显示比较表格
print("算法性能比较表:")
print("=" * 100)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
print(comparison_df.round(4))
# 性能分析
print(f"\n性能分析:")
print("=" * 40)
# 找出最佳算法
best_accuracy = comparison_df.loc[comparison_df['准确率'].idxmax()]
best_f1 = comparison_df.loc[comparison_df['F1分数'].idxmax()]
best_auc = comparison_df.loc[comparison_df['AUC'].idxmax()]
print(f"最高准确率: {best_accuracy['算法']} ({best_accuracy['准确率']:.4f})")
print(f"最高F1分数: {best_f1['算法']} ({best_f1['F1分数']:.4f})")
print(f"最高AUC: {best_auc['算法']} ({best_auc['AUC']:.4f})")
# 训练效率分析
traditional_only = comparison_df[comparison_df['训练时间(秒)'].notna()]
if not traditional_only.empty:
fastest = traditional_only.loc[traditional_only['训练时间(秒)'].idxmin()]
print(f"训练最快: {fastest['算法']} ({fastest['训练时间(秒)']:.2f}秒)")
return comparison_df
def visualize_algorithm_comparison(comparison_df, traditional_results, nn_results, y_test):
"""
可视化算法比较结果
学习如何用图表清晰地展示算法比较结果
"""
print("\n" + "=" * 60)
print("步骤3.4: 算法比较可视化")
print("=" * 60)
# 创建子图
fig, axes = plt.subplots(2, 3, figsize=(18, 12))
fig.suptitle('机器学习算法性能比较', fontsize=16, fontweight='bold')
# 1. 主要性能指标比较
metrics = ['准确率', '精确率', '召回率', 'F1分数', 'AUC']
x_pos = np.arange(len(comparison_df))
ax1 = axes[0, 0]
width = 0.15
for i, metric in enumerate(metrics):
ax1.bar(x_pos + i * width, comparison_df[metric], width, label=metric)
ax1.set_title('主要性能指标比较')
ax1.set_xlabel('算法')
ax1.set_ylabel('性能分数')
ax1.set_xticks(x_pos + width * 2)
ax1.set_xticklabels(comparison_df['算法'], rotation=45)
ax1.legend()
ax1.grid(True, alpha=0.3)
# 2. 准确率对比
ax2 = axes[0, 1]
colors = ['skyblue', 'lightgreen', 'lightcoral', 'gold', 'plum']
bars = ax2.bar(comparison_df['算法'], comparison_df['准确率'], color=colors)
ax2.set_title('算法准确率对比')
ax2.set_ylabel('准确率')
ax2.set_xticklabels(comparison_df['算法'], rotation=45)
ax2.grid(True, alpha=0.3)
# 在柱子上添加数值
for bar, acc in zip(bars, comparison_df['准确率']):
ax2.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.005,
f'{acc:.3f}', ha='center', va='bottom', fontweight='bold')
# 3. F1分数对比
ax3 = axes[0, 2]
bars = ax3.bar(comparison_df['算法'], comparison_df['F1分数'], color=colors)
ax3.set_title('F1分数对比 (精确率与召回率的平衡)')
ax3.set_ylabel('F1分数')
ax3.set_xticklabels(comparison_df['算法'], rotation=45)
ax3.grid(True, alpha=0.3)
for bar, f1 in zip(bars, comparison_df['F1分数']):
ax3.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.005,
f'{f1:.3f}', ha='center', va='bottom', fontweight='bold')
# 4. 训练时间比较 (仅传统算法)
ax4 = axes[1, 0]
traditional_df = comparison_df[comparison_df['训练时间(秒)'].notna()]
if not traditional_df.empty:
bars = ax4.bar(traditional_df['算法'], traditional_df['训练时间(秒)'], color=colors[:len(traditional_df)])
ax4.set_title('训练时间比较 (传统算法)')
ax4.set_ylabel('训练时间 (秒)')
ax4.set_xticklabels(traditional_df['算法'], rotation=45)
ax4.grid(True, alpha=0.3)
for bar, time_val in zip(bars, traditional_df['训练时间(秒)']):
ax4.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.01,
f'{time_val:.2f}', ha='center', va='bottom', fontweight='bold')
# 5. ROC曲线比较
ax5 = axes[1, 1]
colors_roc = ['blue', 'green', 'red', 'orange', 'purple']
# 绘制传统算法的ROC曲线
for i, (name, results) in enumerate(traditional_results.items()):
if results['probabilities'] is not None:
fpr, tpr, _ = roc_curve(y_test, results['probabilities'])
auc_score = results['auc']
ax5.plot(fpr, tpr, color=colors_roc[i], lw=2,
label=f'{name} (AUC = {auc_score:.3f})')
# 绘制神经网络的ROC曲线
fpr_nn, tpr_nn, _ = roc_curve(y_test, nn_results['probabilities'])
ax5.plot(fpr_nn, tpr_nn, color=colors_roc[4], lw=2,
label=f'Neural Network (AUC = {nn_results["auc"]:.3f})')
# 绘制对角线(随机分类器)
ax5.plot([0, 1], [0, 1], 'k--', lw=1, label='随机分类器 (AUC = 0.5)')
ax5.set_title('ROC曲线比较')
ax5.set_xlabel('假阳性率 (False Positive Rate)')
ax5.set_ylabel('真阳性率 (True Positive Rate)')
ax5.legend(loc='lower right')
ax5.grid(True, alpha=0.3)
# 6. 混淆矩阵热图 (最佳算法)
ax6 = axes[1, 2]
best_algorithm = comparison_df.loc[comparison_df['F1分数'].idxmax(), '算法']
if best_algorithm == 'Neural Network':
best_predictions = nn_results['predictions']
else:
best_predictions = traditional_results[best_algorithm]['predictions']
cm = confusion_matrix(y_test, best_predictions)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax6)
ax6.set_title(f'混淆矩阵 - {best_algorithm} (最佳F1)')
ax6.set_xlabel('预测标签')
ax6.set_ylabel('真实标签')
plt.tight_layout()
plt.show()
print("图表说明:")
print("1. 主要性能指标比较: 展示所有算法在五个关键指标上的表现")
print("2. 准确率对比: 直观显示各算法的预测准确程度")
print("3. F1分数对比: 平衡考虑精确率和召回率的综合指标")
print("4. 训练时间比较: 算法的计算效率对比")
print("5. ROC曲线: 不同阈值下的分类性能,AUC越大越好")
print("6. 混淆矩阵: 最佳算法的详细分类结果")
def analyze_algorithm_strengths_weaknesses(comparison_df, traditional_results, nn_results):
"""
分析各算法的优缺点
学习如何深入理解不同算法的特点
"""
print("\n" + "=" * 60)
print("步骤3.5: 算法优缺点分析")
print("=" * 60)
algorithm_analysis = {
'Logistic Regression': {
'优点': ['训练快速', '模型简单可解释', '不容易过拟合', '适合线性可分问题'],
'缺点': ['无法处理复杂非线性关系', '对特征缩放敏感', '假设特征独立'],
'适用场景': '数据量小、需要快速训练、需要模型解释性的场景'
},
'Random Forest': {
'优点': ['处理非线性关系强', '不容易过拟合', '可以处理缺失值', '提供特征重要性'],
'缺点': ['模型较大', '训练时间较长', '在噪声数据上表现一般'],
'适用场景': '数据复杂、特征维度高、需要稳定性能的场景'
},
'Gradient Boosting': {
'优点': ['通常准确率最高', '可以逐步优化', '处理复杂模式能力强'],
'缺点': ['容易过拟合', '训练时间长', '对参数敏感', '需要调参'],
'适用场景': '追求最高准确率、有充足时间调参的场景'
},
'Support Vector Machine': {
'优点': ['在高维空间表现好', '内存效率高', '适合小样本'],
'缺点': ['训练时间长', '对参数敏感', '不直接提供概率输出'],
'适用场景': '高维数据、小样本、需要稳定性能的场景'
},
'Neural Network': {
'优点': ['学习能力强', '可以自动特征提取', '适合复杂模式'],
'缺点': ['需要大量数据', '训练时间长', '需要调参', '黑盒模型'],
'适用场景': '大数据量、复杂模式、有充足计算资源的场景'
}
}
print("各算法详细分析:")
print("=" * 40)
for algorithm in comparison_df['算法']:
if algorithm in algorithm_analysis:
analysis = algorithm_analysis[algorithm]
# 获取性能数据
perf_data = comparison_df[comparison_df['算法'] == algorithm].iloc[0]
print(f"\n📊 {algorithm}")
print("-" * 30)
print(
f"性能表现: 准确率={perf_data['准确率']:.4f}, F1={perf_data['F1分数']:.4f}, AUC={perf_data['AUC']:.4f}")
print("✅ 优点:")
for advantage in analysis['优点']:
print(f" • {advantage}")
print("❌ 缺点:")
for disadvantage in analysis['缺点']:
print(f" • {disadvantage}")
print(f"🎯 适用场景: {analysis['适用场景']}")
def provide_algorithm_recommendations(comparison_df):
"""
提供算法选择建议
学习如何根据实际需求选择最合适的算法
"""
print("\n" + "=" * 60)
print("步骤3.6: 算法选择建议")
print("=" * 60)
# 找出各项指标的最佳算法
best_accuracy = comparison_df.loc[comparison_df['准确率'].idxmax()]
best_f1 = comparison_df.loc[comparison_df['F1分数'].idxmax()]
best_precision = comparison_df.loc[comparison_df['精确率'].idxmax()]
best_recall = comparison_df.loc[comparison_df['召回率'].idxmax()]
print("🏆 推荐算法 (根据不同需求):")
print("=" * 40)
print(f"1. 整体性能最佳: {best_f1['算法']}")
print(f" F1分数: {best_f1['F1分数']:.4f} (平衡了精确率和召回率)")
print(f" 推荐理由: 在水质预测任务中需要平衡准确识别可饮用水和避免误判")
print(f"\n2. 准确率最高: {best_accuracy['算法']}")
print(f" 准确率: {best_accuracy['准确率']:.4f}")
print(f" 推荐理由: 如果主要关心整体预测正确率")
print(f"\n3. 精确率最高: {best_precision['算法']}")
print(f" 精确率: {best_precision['精确率']:.4f}")
print(f" 推荐理由: 如果要避免将不安全的水误判为安全 (保守策略)")
print(f"\n4. 召回率最高: {best_recall['算法']}")
print(f" 召回率: {best_recall['召回率']:.4f}")
print(f" 推荐理由: 如果要确保找出所有安全的水源 (宽松策略)")
# 根据实际场景提供建议
print(f"\n🎯 实际应用建议:")
print("=" * 40)
print("场景1 - 实时水质监测系统:")
fastest_traditional = comparison_df[comparison_df['训练时间(秒)'].notna()]
if not fastest_traditional.empty:
fastest = fastest_traditional.loc[fastest_traditional['训练时间(秒)'].idxmin()]
print(f" 推荐: {fastest['算法']} (训练快速: {fastest['训练时间(秒)']:.2f}秒)")
print(f"场景2 - 高精度离线分析:")
print(f" 推荐: {best_f1['算法']} (最佳综合性能)")
print(f"场景3 - 移动端应用:")
print(f" 推荐: Logistic Regression (模型小巧,部署简单)")
print(f"场景4 - 研究分析:")
print(f" 推荐: Random Forest (提供特征重要性分析)")
def save_comparison_results(comparison_df, traditional_results, nn_results, output_dir="algorithm_comparison"):
"""
保存算法比较结果
便于后续分析和报告使用
"""
import os
print("\n" + "=" * 60)
print("步骤3.7: 保存比较结果")
print("=" * 60)
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 保存比较表格
comparison_df.to_csv(f"{output_dir}/algorithm_comparison.csv", index=False)
# 保存所有模型
all_results = {
'traditional_models': traditional_results,
'neural_network': nn_results,
'comparison_table': comparison_df
}
torch.save(all_results, f"{output_dir}/all_results.pt")
# 保存最佳模型
best_algorithm = comparison_df.loc[comparison_df['F1分数'].idxmax(), '算法']
if best_algorithm != 'Neural Network':
best_model = traditional_results[best_algorithm]['model']
joblib.dump(best_model, f"{output_dir}/best_traditional_model.pkl")
print(f"✓ 比较结果已保存到 {output_dir}/ 目录:")
print(f" - algorithm_comparison.csv: 性能比较表格")
print(f" - all_results.pt: 所有模型和结果")
print(f" - best_traditional_model.pkl: 最佳传统模型")
print(f"\n最佳算法: {best_algorithm}")
def main():
"""
主函数 - 执行完整的算法比较流程
这是选择最优模型的关键步骤
"""
print("🎯 水质安全预测项目 - 算法性能比较")
print("📚 学习目标: 掌握如何系统地比较不同机器学习算法")
print("包括: 多算法训练、性能评估、结果分析、算法选择策略")
try:
# 步骤1: 加载数据和已训练模型
X, y, nn_model, nn_metrics = load_training_data()
if X is None:
return
# 步骤2: 划分数据
X_train, X_test, y_train, y_test = split_data_for_comparison(X, y)
# 步骤3: 创建传统机器学习模型
traditional_models = create_traditional_models()
# 步骤4: 训练和评估传统模型
traditional_results = train_and_evaluate_traditional_models(
traditional_models, X_train, X_test, y_train, y_test
)
# 步骤5: 评估神经网络
nn_results = evaluate_neural_network(nn_model, X_test, y_test)
# 步骤6: 创建比较表格
comparison_df = create_comparison_table(traditional_results, nn_results)
# 步骤7: 可视化比较结果
visualize_algorithm_comparison(comparison_df, traditional_results, nn_results, y_test)
# 步骤8: 分析算法优缺点
analyze_algorithm_strengths_weaknesses(comparison_df, traditional_results, nn_results)
# 步骤9: 提供选择建议
provide_algorithm_recommendations(comparison_df)
# 步骤10: 保存结果
save_comparison_results(comparison_df, traditional_results, nn_results)
print("\n" + "🎉" * 20)
print("算法性能比较完成!")
print("下一步: 进行步骤4 - 理解和解释模型结果")
print("🎉" * 20)
except Exception as e:
print(f"❌ 比较过程中发生错误: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()
"""
📝 学习总结:
通过这个步骤,你学会了:
1. 如何系统地比较多种机器学习算法
2. 不同算法的特点、优势和局限性
3. 使用多种评估指标全面评估模型性能
4. 通过可视化清晰展示算法比较结果
5. 根据实际需求选择最适合的算法
6. 理解算法选择需要权衡的因素
这些技能帮助你在实际项目中做出明智的算法选择决策!
"""