-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
144 lines (110 loc) · 4.51 KB
/
utils.py
File metadata and controls
144 lines (110 loc) · 4.51 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
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from imblearn.over_sampling import *
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_curve, auc
from aeon.classification.interval_based import TimeSeriesForestClassifier
from aeon.classification.hybrid import HIVECOTEV2
from aeon.classification.convolution_based import MultiRocketHydraClassifier
from aeon.classification.sklearn import RotationForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.base import BaseEstimator, TransformerMixin
sns.set_context("paper")
from config import Config
def plot_decision_function(X, y, clf, ax, title=None):
plot_step = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(
np.arange(x_min, x_max, plot_step), np.arange(y_min, y_max, plot_step)
)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, alpha=0.4)
ax.scatter(X[:, 0], X[:, 1], alpha=0.8, c=y, edgecolor="k")
if title is not None:
ax.set_title(title)
def metric_factors(y_true, y_pred, y_pred_proba, positive_class=1, verbose=True):
# 1. Accuracy
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred, pos_label=positive_class, zero_division=0)
recall = recall_score(y_true, y_pred, pos_label=positive_class, zero_division=0)
# 2. F1 Score
f1 = f1_score(y_true, y_pred)
# 3. ROC Curve
fpr, tpr, thresholds = roc_curve(y_true, y_pred_proba[:, 1])
# 4. AUC
roc_auc_value = auc(fpr, tpr)
# Plot ROC Curve
if verbose:
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
print(f"AUC: {roc_auc_value:.4f}")
plt.figure(figsize=(10, 6))
plt.plot(fpr, tpr, color='blue', label=f'ROC curve (area = {roc_auc_value:.2f})')
plt.plot([0, 1], [0, 1], color='red', linestyle='--') # Diagonal line (random classifier)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc='lower right')
plt.grid()
plt.show()
return accuracy, precision, recall, f1, roc_auc_value, fpr, tpr
class None_sampler:
def __init__(self):
pass
def fit_resample(self, X, y):
return X, y
class SqueezeTransformer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self # 无需拟合,直接返回
def transform(self, X):
return np.squeeze(X) # 压缩数组中的维度
class OverSamplingMethods:
"""
over-sampling methods include 'ADASYN', 'RandomOverSampler', 'KMeansSMOTE', 'SMOTE',
'BorderlineSMOTE', 'SVMSMOTE', 'SMOTENC', 'SMOTEN'
"""
def __init__(self):
self.config = Config()
def none_sampling(self):
return None_sampler()
def ros(self):
return RandomOverSampler(random_state=self.config.seed)
def rose(self):
return RandomOverSampler(random_state=self.config.seed, shrinkage={1: 2.})
def adasyn(self):
return ADASYN(random_state=self.config.seed, n_neighbors=5)
def smote(self):
return SMOTE(random_state=self.config.seed, k_neighbors=5)
class ClassificationMetrics:
"""
over-sampling methods include 'ADASYN', 'RandomOverSampler', 'KMeansSMOTE', 'SMOTE',
'BorderlineSMOTE', 'SVMSMOTE', 'SMOTENC', 'SMOTEN'
"""
def __init__(self):
self.config = Config()
def tsf_classifier(self):
return TimeSeriesForestClassifier(n_estimators=50, random_state=self.config.seed)
def hc2(self):
return HIVECOTEV2(random_state=self.config.seed)
def multi_rocket_hydra(self):
return MultiRocketHydraClassifier(random_state=self.config.seed)
def rotation_forest(self):
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('squeeze', SqueezeTransformer()),
('classifier', RotationForestClassifier(random_state=self.config.seed))
])
return pipeline
def logistic_regression(self):
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('squeeze', SqueezeTransformer()),
('classifier', LogisticRegression(random_state=self.config.seed))
])
return pipeline