-
Notifications
You must be signed in to change notification settings - Fork 1
/
ts_classification_3.py
176 lines (145 loc) · 4.7 KB
/
ts_classification_3.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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.metrics import (
accuracy_score,
auc,
classification_report,
f1_score,
plot_confusion_matrix,
roc_auc_score,
roc_curve,
)
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelBinarizer
from tslearn.piecewise import SymbolicAggregateApproximation
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
from tslearn.shapelets import ShapeletModel
from music import MusicDB
"""CLASSIFICAZIONE CON SAX E SHAPLET KNN"""
"""
FILE 3- CLASSIFICAZIONE CON APPROSSIMAZIONE CON SAX E SHAPLET KNN
"""
def draw_confusion_matrix(Clf, X, y):
titles_options = [
("Confusion matrix, without normalization", None),
("KNN-Shaplet-Sax Confusion matrix", "true"),
]
for title, normalize in titles_options:
disp = plot_confusion_matrix(Clf, X, y, cmap="OrRd", normalize=normalize)
disp.ax_.set_title(title)
plt.show()
# Carico il dataframe
musi = MusicDB()
print(musi.df.info())
print(musi.feat["enc_genre"].unique())
X_no = musi.df
y = musi.feat["enc_genre"] # classe targed ovvero genere con l'encoding
# normalizzazione con mean variance
scaler = TimeSeriesScalerMeanVariance()
X_no = pd.DataFrame(
scaler.fit_transform(musi.df.values).reshape(
musi.df.values.shape[0], musi.df.values.shape[1]
)
)
X_no.index = musi.df.index
# approssimazione con sax
sax = SymbolicAggregateApproximation(n_segments=130, alphabet_size_avg=20)
X1 = sax.fit_transform(X_no)
print(X1.shape)
X = np.squeeze(X1)
print(X.shape)
# Classification
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=100, stratify=y
)
"""INSERISCO PARTE SHAPLET"""
n_ts, ts_sz = X_train.shape
n_classes = len(set(y))
# shapelet_sizes = grabocka_params_to_shapelet_size_dict(
# n_ts=n_ts, ts_sz=ts_sz, n_classes=n_classes, l=0.1, r=1
# )
# 6 shaplet da 15 con grabocka
shapelet_sizes = {15: 24}
print("n_ts", n_ts)
print("ts_sz", ts_sz)
print("n_classes", n_classes)
print("shapelet_sizes", shapelet_sizes)
shp_clf = ShapeletModel(
n_shapelets_per_size=shapelet_sizes,
optimizer="sgd",
weight_regularizer=0.01,
max_iter=50,
verbose=1,
)
shp_clf.fit(X_train, y_train)
print("Apply on the test set and evaluate the performance: \n")
y_pred = shp_clf.predict(X_test)
print("Accuracy %s" % accuracy_score(y_test, y_pred))
print("F1-score %s" % f1_score(y_test, y_pred, average=None))
print(classification_report(y_test, y_pred))
"""CLASSIFICATORE"""
print("KNN- Shaplet Based")
X_train2 = shp_clf.transform(X_train)
print("train shape:", X_train2.shape)
X_test2 = shp_clf.transform(X_test)
knn = KNeighborsClassifier(n_neighbors=19, weights="distance", p=1)
# Best parameters: {'n_neighbors': 19, p=1, weights='distance'}
knn.fit(X_train2, y_train)
# Apply on the test set and evaluate the performance
print("Apply on the test set and evaluate the performance-KNN: \n")
y_pred = knn.predict(X_test2)
print("Accuracy %s" % accuracy_score(y_test, y_pred))
print("F1-score %s" % f1_score(y_test, y_pred, average=None))
print(classification_report(y_test, y_pred))
draw_confusion_matrix(knn, X_test2, y_test)
"""GRID SEARCH
print("STA FACENDO LA GRIDSEARCH")
param_list = {
"n_neighbors": list(np.arange(1, 20)),
"weights": ["uniform", "distance"],
"p": [1, 2],
}
# grid search
clf = KNeighborsClassifier()
grid_search = GridSearchCV(clf, param_grid=param_list, scoring="accuracy")
grid_search.fit(X_train2, y_train)
# results of the grid search
print("\033[1m" "Results of the grid search" "\033[0m")
print()
print("Best parameters: %s" % grid_search.best_params_)
print("Best estimator: %s" % grid_search.best_estimator_)
print()
print("Best k ('n_neighbors'): %s" % grid_search.best_params_["n_neighbors"])
print()
"""
"""ROC CURVE"""
lb = LabelBinarizer()
lb.fit(y_test)
lb.classes_.tolist()
fpr = dict()
tpr = dict()
roc_auc = dict()
by_test = lb.transform(y_test)
by_pred = lb.transform(y_pred)
for i in range(8):
fpr[i], tpr[i], _ = roc_curve(by_test[:, i], by_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
roc_auc = roc_auc_score(by_test, by_pred, average=None)
plt.figure(figsize=(8, 5))
for i in range(8):
plt.plot(
fpr[i],
tpr[i],
label="%s ROC curve (area = %0.2f)" % (lb.classes_.tolist()[i], roc_auc[i]),
)
plt.plot([0, 1], [0, 1], "k--")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.title("KNN-Shaplet-Sax Roc-Curve")
plt.xlabel("False Positive Rate", fontsize=10)
plt.ylabel("True Positive Rate", fontsize=10)
plt.tick_params(axis="both", which="major", labelsize=12)
plt.legend(loc="lower right", fontsize=7, frameon=False)
plt.show()