-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.py
More file actions
146 lines (121 loc) · 4.63 KB
/
Copy pathModel.py
File metadata and controls
146 lines (121 loc) · 4.63 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
import numpy as np
from sklearn.svm import SVC
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import LeaveOneOut, cross_val_predict, permutation_test_score
from sklearn.metrics import balanced_accuracy_score, f1_score, classification_report, confusion_matrix
from seaborn import heatmap
import joblib
import json
import os
from datetime import datetime
import logging
import re
# 配置日志
logger = logging.getLogger(__name__)
#-----------------------------------------------
# Load data
#-----------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(
y_true,
y_pred,
class_names,
normalize=True,
title="Confusion Matrix"
):
cm = confusion_matrix(y_true, y_pred)
if normalize:
cm = cm.astype(float) / cm.sum(axis=1, keepdims=True)
plt.figure(figsize=(6, 5))
sns.heatmap(
cm,
annot=True,
fmt=".2f" if normalize else "d",
cmap="Blues",
xticklabels=class_names,
yticklabels=class_names,
cbar=True
)
plt.xlabel("Predicted label")
plt.ylabel("True label")
plt.title(title)
plt.tight_layout()
plt.show()
def load_model_pipeline(model_path, scaler_path, config_path):
"""
加载保存的模型管道
Args:
model_path: 模型文件路径
scaler_path: 标准化器文件路径
config_path: 配置文件路径
Returns:
dict: 包含模型、标准化器和配置信息的字典
"""
model = joblib.load(model_path)
scaler = joblib.load(scaler_path)
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
return {
'model': model,
'scaler': scaler,
'config': config
}
def predict_new_sample(model_pipeline, new_data):
"""
使用保存的模型管道对新样本进行预测
Args:
model_pipeline: 加载的模型管道字典
new_data: 新样本数据 (numpy array 或 pandas DataFrame)
Returns:
numpy array: 预测结果
"""
model = model_pipeline['model']
scaler = model_pipeline['scaler']
# 标准化新数据
if hasattr(new_data, 'values'):
new_data = new_data.values
new_data_scaled = scaler.transform(new_data.reshape(1, -1))
# 进行预测
prediction = model.predict(new_data_scaled)
probabilities = None
# 如果模型支持概率预测,也返回概率
if hasattr(model, 'predict_proba'):
probabilities = model.predict_proba(new_data_scaled)
return {
'prediction': prediction[0],
'probabilities': probabilities[0] if probabilities is not None else None
}
if __name__ == "__main__":
# feature_cols = ['RMS-ym','RMS-by','MAV-ym','MAV-by','MF-ym','RMSF-by','RMSF-mz','RVF-ym','RVF-by','RMSF-dz']
feature_cols = ['RMS-ym','RMS-by','MAV-ym','MAV-by','MF-ym']
label_col = 'hb'
# 演示如何加载和使用保存的模型
print("\n=== Model Loading Demo ===")
final_save_paths = {
'model_path': 'saved_models/svm_loocv_20260110_121421.pkl',
'scaler_path': 'saved_models/svm_loocv_scaler_20260110_121421.pkl',
'config_path': 'saved_models/svm_loocv_config_20260110_121421.json',
}
try:
loaded_pipeline = load_model_pipeline(final_save_paths['model_path'],
final_save_paths['scaler_path'],
final_save_paths['config_path'])
print("Model loaded successfully!")
print(f"Model type: {loaded_pipeline['config']['model_type']}")
print(f"Features: {loaded_pipeline['config']['feature_columns']}")
# 演示对新样本的预测
X = np.ndarray([[0.1,0.2,0.3,0.4,0.5],[0,0,0,0,0,]])
if len(X) > 0:
sample = X[0] # 使用第一个样本作为示例
prediction = predict_new_sample(loaded_pipeline, sample)
print(f"Sample prediction: {prediction['prediction']}")
if prediction['probabilities'] is not None:
print(f"Prediction probabilities: {prediction['probabilities']}")
except Exception as e:
print(f"Error loading model: {e}")