Skip to content

Commit f4a3e5f

Browse files
committed
TS Learn
1 parent 7cc8951 commit f4a3e5f

File tree

2 files changed

+772
-0
lines changed

2 files changed

+772
-0
lines changed
Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from tslearn.utils import to_time_series_dataset"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"X = to_time_series_dataset([[1, 2, 3, 4], [1, 2, 3], [2, 5, 6, 7, 8, 9]])"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 3,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"y = [0, 0, 1]"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 4,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"data": {
37+
"text/plain": [
38+
"array([[[ 1.],\n",
39+
" [ 2.],\n",
40+
" [ 3.],\n",
41+
" [ 4.],\n",
42+
" [nan],\n",
43+
" [nan]],\n",
44+
"\n",
45+
" [[ 1.],\n",
46+
" [ 2.],\n",
47+
" [ 3.],\n",
48+
" [nan],\n",
49+
" [nan],\n",
50+
" [nan]],\n",
51+
"\n",
52+
" [[ 2.],\n",
53+
" [ 5.],\n",
54+
" [ 6.],\n",
55+
" [ 7.],\n",
56+
" [ 8.],\n",
57+
" [ 9.]]])"
58+
]
59+
},
60+
"execution_count": 4,
61+
"metadata": {},
62+
"output_type": "execute_result"
63+
}
64+
],
65+
"source": [
66+
"X"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 5,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"[0, 0, 1]"
78+
]
79+
},
80+
"execution_count": 5,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"y"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 6,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"from tslearn.clustering import GlobalAlignmentKernelKMeans"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 7,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"gak_km = GlobalAlignmentKernelKMeans(n_clusters=2)"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 8,
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"name": "stdout",
114+
"output_type": "stream",
115+
"text": [
116+
"Resumed because of empty cluster\n",
117+
"2.000 --> 2.000 --> \n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"labels_gak = gak_km.fit_predict(X)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 9,
128+
"metadata": {},
129+
"outputs": [
130+
{
131+
"data": {
132+
"text/plain": [
133+
"array([1, 0, 1])"
134+
]
135+
},
136+
"execution_count": 9,
137+
"metadata": {},
138+
"output_type": "execute_result"
139+
}
140+
],
141+
"source": [
142+
"labels_gak"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 10,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"from tslearn.clustering import TimeSeriesKMeans"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 11,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"km = TimeSeriesKMeans(n_clusters=2, metric=\"dtw\")"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 12,
166+
"metadata": {},
167+
"outputs": [
168+
{
169+
"name": "stdout",
170+
"output_type": "stream",
171+
"text": [
172+
"Resumed because of empty cluster\n",
173+
"Resumed because of empty cluster\n",
174+
"Resumed because of empty cluster\n",
175+
"5.000 --> 1.056 --> 1.056 --> \n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"labels = km.fit_predict(X)"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 13,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"data": {
190+
"text/plain": [
191+
"array([1, 1, 0])"
192+
]
193+
},
194+
"execution_count": 13,
195+
"metadata": {},
196+
"output_type": "execute_result"
197+
}
198+
],
199+
"source": [
200+
"labels"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 14,
206+
"metadata": {},
207+
"outputs": [],
208+
"source": [
209+
"km_bis = TimeSeriesKMeans(n_clusters=2, metric=\"softdtw\")"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 15,
215+
"metadata": {},
216+
"outputs": [
217+
{
218+
"name": "stdout",
219+
"output_type": "stream",
220+
"text": [
221+
"Resumed because of empty cluster\n",
222+
"Resumed because of empty cluster\n",
223+
"Resumed because of empty cluster\n",
224+
"Resumed because of empty cluster\n",
225+
"Resumed because of empty cluster\n",
226+
"Resumed because of empty cluster\n",
227+
"58.996 --> 1.956 --> 1.956 --> 1.956 --> 1.956 --> \n"
228+
]
229+
}
230+
],
231+
"source": [
232+
"labels_bis = km_bis.fit_predict(X)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 16,
238+
"metadata": {},
239+
"outputs": [],
240+
"source": [
241+
"from tslearn.clustering import TimeSeriesKMeans, silhouette_score"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 17,
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"km = TimeSeriesKMeans(n_clusters=2, metric=\"dtw\")"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 18,
256+
"metadata": {},
257+
"outputs": [
258+
{
259+
"name": "stdout",
260+
"output_type": "stream",
261+
"text": [
262+
"5.000 --> 1.056 --> 1.056 --> \n"
263+
]
264+
}
265+
],
266+
"source": [
267+
"labels = km.fit_predict(X)"
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": 19,
273+
"metadata": {},
274+
"outputs": [
275+
{
276+
"data": {
277+
"text/plain": [
278+
"array([1, 1, 0])"
279+
]
280+
},
281+
"execution_count": 19,
282+
"metadata": {},
283+
"output_type": "execute_result"
284+
}
285+
],
286+
"source": [
287+
"labels "
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": 20,
293+
"metadata": {},
294+
"outputs": [
295+
{
296+
"data": {
297+
"text/plain": [
298+
"0.5875727602071013"
299+
]
300+
},
301+
"execution_count": 20,
302+
"metadata": {},
303+
"output_type": "execute_result"
304+
}
305+
],
306+
"source": [
307+
"silhouette_score(X, labels, metric=\"dtw\")"
308+
]
309+
},
310+
{
311+
"cell_type": "code",
312+
"execution_count": 21,
313+
"metadata": {},
314+
"outputs": [],
315+
"source": [
316+
"from tslearn.barycenters import dtw_barycenter_averaging"
317+
]
318+
},
319+
{
320+
"cell_type": "code",
321+
"execution_count": 22,
322+
"metadata": {},
323+
"outputs": [],
324+
"source": [
325+
"bar = dtw_barycenter_averaging(X, barycenter_size=3)"
326+
]
327+
},
328+
{
329+
"cell_type": "code",
330+
"execution_count": 23,
331+
"metadata": {},
332+
"outputs": [],
333+
"source": [
334+
"from tslearn.barycenters import softdtw_barycenter"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": 24,
340+
"metadata": {},
341+
"outputs": [],
342+
"source": [
343+
"from tslearn.utils import ts_zeros"
344+
]
345+
},
346+
{
347+
"cell_type": "code",
348+
"execution_count": 25,
349+
"metadata": {},
350+
"outputs": [],
351+
"source": [
352+
"initial_barycenter = ts_zeros(sz=5)"
353+
]
354+
},
355+
{
356+
"cell_type": "code",
357+
"execution_count": 26,
358+
"metadata": {},
359+
"outputs": [],
360+
"source": [
361+
"bar = softdtw_barycenter(X, init=initial_barycenter)"
362+
]
363+
}
364+
],
365+
"metadata": {
366+
"kernelspec": {
367+
"display_name": "Python 3",
368+
"language": "python",
369+
"name": "python3"
370+
},
371+
"language_info": {
372+
"codemirror_mode": {
373+
"name": "ipython",
374+
"version": 3
375+
},
376+
"file_extension": ".py",
377+
"mimetype": "text/x-python",
378+
"name": "python",
379+
"nbconvert_exporter": "python",
380+
"pygments_lexer": "ipython3",
381+
"version": "3.6.8"
382+
}
383+
},
384+
"nbformat": 4,
385+
"nbformat_minor": 2
386+
}

0 commit comments

Comments
 (0)