-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatio-time cluster.py
168 lines (131 loc) · 5.6 KB
/
spatio-time cluster.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
from __future__ import division
import matplotlib.pylab as plt
import numpy as np
import random
import pandas as pd
from scipy import spatial
from sklearn.decomposition import PCA
from sklearn import preprocessing
class ts_cluster(object):
def __init__(self,num_clust=100):
'''
num_clust is the number of clusters for the k-means algorithm
assignments holds the assignments of data points (indices) to clusters
centroids holds the centroids of the clusters
'''
self.num_clust=num_clust
self.assignments={}
self.centroids=[]
def compa_clust(self,s1,centroid,w):
centroid_part = centroid[:,:5]
self.assign = pd.Series([[] for i in range(len(centroid))],index=np.arange(len(centroid)))
for ind,i in enumerate(s1):
min_dist=float('inf')
#closest_clust=None
for c_ind,j in enumerate(centroid_part):
if self.LB_Keogh(i,j,5)<min_dist:
cur_dist=self.SpatioTemporalDis(i, j, w)
if cur_dist<min_dist:
min_dist=cur_dist
closest_clust=c_ind
self.assign[closest_clust].append(ind)
print self.assign
#print s1[1]
self.s2 = pd.Series([[] for i in range(len(s1))],index=np.arange(len(s1)))
#print self.s2
for key in self.assign.index:
for k in self.assign[key]:
self.s2[k] = centroid[key,6:].tolist()
#print self.s2[k]
self.s2 = np.array(self.s2)
return self.s2
def k_means_clust(self,data,num_iter,w,progress=True):
'''
k-means clustering algorithm for time series data. dynamic time warping Euclidean distance
used as default similarity measure.
'''
self.centroids=random.sample(data,self.num_clust)
print len(self.centroids)
for n in range(num_iter):
if progress:
print 'iteration '+str(n+1)
self.assignments={}
for ind,i in enumerate(data):
min_dist=float('inf')
#closest_clust=None
for c_ind,j in enumerate(self.centroids):
if self.LB_Keogh(i,j,5)<min_dist:
cur_dist=self.SpatioTemporalDis(i, j, w)
if cur_dist<min_dist:
min_dist=cur_dist
closest_clust=c_ind
if closest_clust in self.assignments:
self.assignments[closest_clust].append(ind)
else:
self.assignments[closest_clust]=[]
print len(self.assignments)
#recalculate centroids of clusters
for key in self.assignments:
clust_sum=0
for k in self.assignments[key]:
clust_sum=clust_sum+data[k]
self.centroids[key]=[m/len(self.assignments[key]) for m in clust_sum]
def get_centroids(self):
return self.centroids
def get_assignments(self):
return self.assignments
def plot_centroids(self):
for i in self.centroids:
plt.plot(i)
plt.show()
def SpatioTemporalDis(self,s1,s2,w):
f1,f2 = s1[0:2],s2[0:2]
v1,v2 = s1[2:],s2[2:]
disInvari = self.InvarDistance(f1, f2)
disVari = self.DTWDistance(v1, v2, w)
return np.sqrt(disInvari+disVari)
def InvarDistance(self,f1,f2):
'''calculates the invariant features distance using Euclidean distance'''
dis = spatial.distance.euclidean(f1, f2)
return dis
def DTWDistance(self,s1,s2,w=None):
'''
Calculates dynamic time warping Euclidean distance between two
sequences. Option to enforce locality constraint for window w.
'''
DTW={}
if w:
w = max(w, abs(len(s1)-len(s2)))
for i in range(-1,len(s1)):
for j in range(-1,len(s2)):
DTW[(i, j)] = float('inf')
else:
for i in range(len(s1)):
DTW[(i, -1)] = float('inf')
for i in range(len(s2)):
DTW[(-1, i)] = float('inf')
DTW[(-1, -1)] = 0
for i in range(len(s1)):
if w:
for j in range(max(0, i-w), min(len(s2), i+w)):
dist= (s1[i]-s2[j])**2
DTW[(i, j)] = dist + min(DTW[(i-1, j)],DTW[(i, j-1)], DTW[(i-1, j-1)])
else:
for j in range(len(s2)):
dist= (s1[i]-s2[j])**2
DTW[(i, j)] = dist + min(DTW[(i-1, j)],DTW[(i, j-1)], DTW[(i-1, j-1)])
return (DTW[len(s1)-1, len(s2)-1])
def LB_Keogh(self,s1,s2,r):
'''
Calculates LB_Keough lower bound to dynamic time warping. Linear
complexity compared to quadratic complexity of dtw.
'''
LB_sum=0
for ind,i in enumerate(s1):
lower_bound=min(s2[(ind-r if ind-r>=0 else 0):(ind+r)])
upper_bound=max(s2[(ind-r if ind-r>=0 else 0):(ind+r)])
if i>upper_bound:
LB_sum=LB_sum+(i-upper_bound)**2
elif i<lower_bound:
LB_sum=LB_sum+(i-lower_bound)**2
return np.sqrt(LB_sum)