-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNN_DDTW.py
More file actions
97 lines (72 loc) · 2.53 KB
/
KNN_DDTW.py
File metadata and controls
97 lines (72 loc) · 2.53 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
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix
from sklearn.model_selection import cross_val_predict, KFold
import sklearn
import numpy as np
import os
import sys
import time
from datetime import datetime
# define a list of datasets
datasets = [
"Control_charts",
"ETCHING_Multivar",
"Hydraulic_systems_10HZ_Multivar",
"Hydraulic_systems_100HZ_Multivar",
"Gas_sensors_home_activity",
# "CWRU_12k_DE_univar",
# "CWRU_12k_DE_multivar",
# "CWRU_12k_FE_univar",
# "CWRU_12k_FE_multivar",
# "CWRU_48k_DE_univar",
# "CWRU_48k_DE_multivar",
# "MFPT_48KHZ_Univar",
# "MFPT_96KHZ_Univar",
# "PHM2022_Multivar",
# "PHM2022_Univar_PIN",
# "PHM2022_Univar_PO",
# "PHM2022_Univar_PDIN",
# "BEARING_Univar",
#"PADERBORN_64KHZ_Univar",
# "PADERBORN_4KHZ_Univar",
# "PADERBORN_64KHZ_Multivar",
# "PADERBORN_4KHZ_Multivar",
]
datasets_path = "../datasets"
print(f"We are going to work on {len(datasets)} datasets!")
for dataset in datasets:
Dataset_name = dataset + "_Dataset"
Dataset = np.load(datasets_path + "/" + Dataset_name + ".npy", mmap_mode='r')
print(Dataset.shape)
Labels_name = dataset + "_Labels"
Labels = np.load(datasets_path + "/" + Labels_name + ".npy", mmap_mode='r')
# change this directory for your machine
root_dir = './'
# define a list of algorithms
algorirhms_path = "./classifiers"
from classifiers import TSKNN_module
# define the number of folds
n_folds = 5
# perform cross-validation for each dataset and algorithm combination
for dataset in datasets:
Dataset_name = dataset + "_Dataset"
Dataset = np.load(datasets_path + "/" + Dataset_name + ".npy", mmap_mode='r')
start = time.time() ##Start timing
start_formated = datetime.fromtimestamp(start).strftime('%Y-%m-%d %H:%M:%S') #change from epoch format to date format
print(f"Starting to work on {Dataset_name} at {start_formated}")
print(f"The shape of the dataset is:{Dataset.shape}")
Labels_name = dataset + "_Labels"
Labels = np.load(datasets_path + "/" + Labels_name + ".npy", mmap_mode='r')
# Create a folder for results
results_path = root_dir + "Results/" + Dataset_name
if os.path.exists(results_path):
pass
else:
try:
os.makedirs(results_path)
except:
# in case another machine created the path meanwhile !:(
pass
#Run The DDTW Module
TSKNN_module.KNN(results_path, Dataset_name, Dataset, Labels, dis= 'ddtw')
print(f"Working on {Dataset_name} finished successfully!")
print("KNN-DDTW algorithm finished!")