-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathts_analysis.py
More file actions
179 lines (147 loc) · 4.37 KB
/
Copy pathts_analysis.py
File metadata and controls
179 lines (147 loc) · 4.37 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
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
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from tslearn.clustering import TimeSeriesKMeans
from tslearn.piecewise import (
OneD_SymbolicAggregateApproximation,
PiecewiseAggregateApproximation,
SymbolicAggregateApproximation,
)
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
from music import MusicDB
# Carico musi come dataframe 62 rows
musi = MusicDB()
musi.df.info()
print(musi.df)
print("{1} features for {0} tracks".format(*musi.df.shape))
"""
# First plot of musi
plt.plot(musi.df)
plt.title("Music small 62 features")
plt.show()
# Looking at some TS
x = musi.df[139]
y = musi.df[2]
z = musi.df[5]
x.plot()
y.plot()
z.plot()
plt.title("Some features")
plt.show()
"""
# Visualize con seaborn
sns.set(rc={"figure.figsize": (11, 4)})
plt.plot(musi.df, linewidth=0.5)
plt.show()
"""
# Ascoltare le canzoni
filename = musi.df[2]
print("File: {}".format(filename))
x, sr = librosa.load(filename, sr=None, mono=True)
print("Duration: {:.2f}s, {} samples".format(x.shape[-1] / sr, x.size))
start, end = 7, 17
ipd.Audio(data=x[start * sr : end * sr], rate=sr)
""" # ESTRAZIONE FEATURES
"""
def calculate_features(values):
features = {
"avg": np.mean(values),
"std": np.std(values),
"var": np.var(values),
"med": np.median(values),
"10p": np.percentile(values, 10),
"25p": np.percentile(values, 25),
"50p": np.percentile(values, 50),
"75p": np.percentile(values, 75),
"90p": np.percentile(values, 90),
"iqr": np.percentile(values, 75) - np.percentile(values, 25),
"cov": 1.0 * np.mean(values) / np.std(values),
"skw": stats.skew(values),
"kur": stats.kurtosis(values),
}
return features
features = calculate_features(musi.df)
print("Features", features)
"""
"""Time series Approximation"""
scaler = TimeSeriesScalerMeanVariance(mu=0.0, std=1.0) # Rescale time series
ts = scaler.fit_transform(musi.df.values.reshape(1, -1))
# PAA transform (and inverse transform) of the data
n_paa_segments = 50
paa = PiecewiseAggregateApproximation(n_segments=n_paa_segments)
ts_paa = paa.fit_transform(ts)
paa_dataset_inv = paa.inverse_transform(ts_paa)
# SAX transform
n_sax_symbols = 50
sax = SymbolicAggregateApproximation(
n_segments=n_paa_segments, alphabet_size_avg=n_sax_symbols
)
ts_sax = sax.fit_transform(ts)
sax_dataset_inv = sax.inverse_transform(ts_sax)
# 1d-SAX transform
n_sax_symbols_avg = 100
n_sax_symbols_slope = 40
one_d_sax = OneD_SymbolicAggregateApproximation(
n_segments=n_paa_segments,
alphabet_size_avg=n_sax_symbols_avg,
alphabet_size_slope=n_sax_symbols_slope,
)
ts_sax1d = one_d_sax.fit_transform(ts)
one_d_sax_dataset_inv = one_d_sax.inverse_transform(ts_sax1d)
"""
plt.figure()
plt.subplot(2, 2, 1) # First, raw time series
plt.plot(ts[0].ravel(), "b-")
plt.title("Raw time series")
plt.subplot(2, 2, 2) # Second, PAA
plt.plot(ts[0].ravel(), "b-", alpha=0.4)
plt.plot(paa_dataset_inv[0].ravel(), "b-")
plt.title("PAA")
plt.subplot(2, 2, 3) # Then SAX
plt.plot(ts[0].ravel(), "b-", alpha=0.4)
plt.plot(sax_dataset_inv[0].ravel(), "b-")
plt.title("SAX, %d symbols" % n_sax_symbols)
plt.subplot(2, 2, 4) # Finally, 1d-SAX
plt.plot(ts[0].ravel(), "b-", alpha=0.4)
plt.plot(one_d_sax_dataset_inv[0].ravel(), "b-")
plt.title(
"1d-SAX, %d symbols"
"(%dx%d)"
% (n_sax_symbols_avg * n_sax_symbols_slope, n_sax_symbols_avg, n_sax_symbols_slope)
)
plt.tight_layout()
plt.show()
"""
plt.plot(ts[0].ravel())
plt.title("normale dataset")
plt.show()
"""
ts1_paa = paa.fit_transform(ts)
plt.plot(paa.inverse_transform(ts1_paa)[0].ravel())
plt.title("PAA inverse")
plt.show()
"""
ts1_sax = sax.fit_transform(ts)
plt.plot(sax.inverse_transform(ts1_sax)[0].ravel())
plt.title("Saxxx")
plt.show()
"""
ts1_sax1d = one_d_sax.fit_transform(ts)
plt.plot(one_d_sax.inverse_transform(ts1_sax1d)[0].ravel())
plt.title("one-d saxxx")
plt.show()
"""
"""K-Means with Sax"""
km = TimeSeriesKMeans(n_clusters=3, metric="euclidean", max_iter=5, random_state=0)
km.fit(ts1_sax)
plt.plot(km.cluster_centers_.reshape(ts1_sax.shape[1], 3))
plt.title("Cluster with k=3 and sax approximation")
plt.show()
hist, bins = np.histogram(km.labels_, bins=range(0, len(set(km.labels_)) + 1))
print("centers", km.cluster_centers_.shape)
plt.plot(np.squeeze(km.cluster_centers_).T)
plt.show()
print()
print("Labels: ", km.labels_)
print()
print("SSE: ", km.inertia_)