-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path8_lab_length_ml.py
256 lines (194 loc) · 7.29 KB
/
8_lab_length_ml.py
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
def calc_metrics_binary(model, X_test, y_test):
from sklearn.metrics import (
classification_report,
accuracy_score,
f1_score,
roc_auc_score,
recall_score,
precision_score,
)
y_pred = model.predict(X_test)
report = classification_report(
y_test, y_pred, target_names=["Normal", "Depressed"], digits=4
)
return report
from datetime import datetime
def train_TimeSeriesForest(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# base
print("***TimeSeriesForestClassifier***")
from sklearn.pipeline import Pipeline
from sktime.classification.interval_based import TimeSeriesForestClassifier
from sktime.classification.compose import ColumnEnsembleClassifier
from sktime.transformations.panel.compose import ColumnConcatenator
steps = [
("concatenate", ColumnConcatenator()),
("classify", TimeSeriesForestClassifier(n_estimators=100)),
]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
return clf
def train_ROCKETClassifier(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# 2020
print("***ROCKETClassifier***")
from sktime.classification.kernel_based import ROCKETClassifier
clf = ROCKETClassifier(num_kernels=500)
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
return clf
def train_Signature(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# 2020
print("***SignatureClassifier***")
from sktime.classification.feature_based import SignatureClassifier
clf = SignatureClassifier()
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
return clf
def train_Arsenal(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# uni
print("***Arsenal***")
from sktime.classification.kernel_based import Arsenal
clf = Arsenal(num_kernels=200, n_estimators=5)
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
start_time = datetime.now()
return clf
def train_TSFresh(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# 2018
print("***TSFreshClassifier***")
from sktime.classification.feature_based import TSFreshClassifier
clf = TSFreshClassifier()
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
return clf
def train_HIVECOTEV2(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# No module named sktime.classificastion.shapelet_based.mrseql.mrseql
print("***HIVECOTEV2***")
from sktime.classification.hybrid import HIVECOTEV2
from sktime.contrib.vector_classifiers._rotation_forest import RotationForest
clf = HIVECOTEV2(
stc_params={
"estimator": RotationForest(n_estimators=3),
"n_shapelet_samples": 500,
"max_shapelets": 20,
"batch_size": 100,
},
drcif_params={"n_estimators": 10},
arsenal_params={"num_kernels": 100, "n_estimators": 5},
tde_params={
"n_parameter_samples": 25,
"max_ensemble_size": 5,
"randomly_selected_params": 10,
},
)
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
start_time = datetime.now()
return clf
def train_ShapeletTransform(X_train, y_train, X_test, y_test):
start_time = datetime.now()
# shapelet
print("***ShapeletTransformClassifier***")
from sktime.classification.shapelet_based import ShapeletTransformClassifier
from sktime.contrib.vector_classifiers._rotation_forest import RotationForest
clf = ShapeletTransformClassifier(
estimator=RotationForest(n_estimators=3),
n_shapelet_samples=500,
max_shapelets=20,
batch_size=100,
)
clf.fit(X_train, y_train)
report = calc_metrics_binary(clf, X_test, y_test)
print(report)
print(str(datetime.now() - start_time))
start_time = datetime.now()
return clf
model_list = [
"bst",
"rocket",
"gs",
"hc2",
"tsf"
]
if __name__ == "__main__":
import os
import numpy as np
from sktime.utils.data_io import load_from_tsfile_to_dataframe
for r in range(10):
# data_dir = "swdd-7k_ts_500_500"
data_dir = "dataset/swdd-7k_ts_origin_500_0"
X_train, y_train = load_from_tsfile_to_dataframe(
os.path.join(data_dir, "train.ts")
)
X_test, y_test = load_from_tsfile_to_dataframe(
os.path.join(data_dir, "test.ts")
)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
print(np.unique(y_train))
for i in range(50, 501, 50):
# if flag and i < 500:
# continue
# data_dir = "swdd-7k_ts_500_{}".format(i)
save_dir = "results/swdd-7k_model_500_{}_simple".format(i)
print(data_dir, save_dir)
for cls in model_list:
if cls == "bst":
try:
clf = train_ShapeletTransform(
X_train[:i], y_train[:i], X_test[:i], y_test[:i]
) # need at least one array to concatenate
except Exception as e:
print(e)
elif cls == "rocket":
clf = train_ROCKETClassifier(
X_train[:i], y_train[:i], X_test[:i], y_test[:i]
)
elif cls == "gs":
# try:
# clf = train_TSFresh(
# X_train[:i], y_train[:i], X_test[:i], y_test[:i]
# )
# except Exception as e:
# print(e)
try:
# clf = train_Signature(X_train[:i], y_train[:i], X_test[:i], y_test[:i])
clf = train_Arsenal(
X_train[:i], y_train[:i], X_test[:i], y_test[:i]
)
except Exception as e:
print(e)
elif cls == "hc2":
try:
clf = train_HIVECOTEV2(
X_train[:i], y_train[:i], X_test[:i], y_test[:i]
) # not work
except Exception as e:
print(e)
elif cls == "tsf":
clf = train_TimeSeriesForest(
X_train[:i], y_train[:i], X_test[:i], y_test[:i]
)
# # analyze model
report = calc_metrics_binary(clf, X_test[:i], y_test[:i])
res_save_path = os.path.join(save_dir, cls + ".txt")
with open(res_save_path, "a+") as f:
f.write(report)
f.write("\n" + "*" * 15 + "\n")