-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster.py
168 lines (128 loc) · 4.89 KB
/
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
'''
# cl: 3
# 2016-07-08 https://www.wunderground.com/history/airport/KNYC/2016/7/8/DailyHistory.html?req_city=New+York&req_state=NY&req_statename=New+York&reqdb.zip=10001&reqdb.magic=8&reqdb.wmo=99999
# cl: 2
# 2016-06-22 https://www.wunderground.com/history/airport/KNYC/2016/6/22/DailyHistory.html?req_city=New+York&req_state=NY&req_statename=New+York&reqdb.zip=10001&reqdb.magic=8&reqdb.wmo=99999
'''
import numpy as np
import pandas
import pandas as pd
from scipy.stats.stats import pearsonr
from sklearn import metrics
from sklearn.cluster import AgglomerativeClustering
from scipy.spatial.distance import cosine
from config import DAYS_PATH_IN, CLUSTERED_DAYS_PATH_OUT
from holidays import make_holidays
def cl_metrics(df_mtx, clusters):
labels = clusters.labels_
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
metric = dict()
metric['nclusters'] = n_clusters_
metric['silhouette'] = metrics.silhouette_score(df_mtx, labels)
print(metric)
print(metrics.silhouette_samples(df_mtx,labels).shape)
metric['calinski_harabaz'] = metrics.calinski_harabaz_score(df_mtx, labels)
print(metric)
return metric
# print('Estimated number of clusters: %d' % n_clusters_)
# print("Silhouette Coefficient: %0.3f"
# % metrics.silhouette_score(df_mtx, labels))
#CLUSTERING METHODS
def cluster_agglomeration(df, n_clusters):
df_mtx = df.values
# metric = metrics.pairwise.pairwise_kernels(days, metric=my_dist)
clusters = AgglomerativeClustering(n_clusters=n_clusters, affinity=custom_affinity, linkage = 'average')
clusters = clusters.fit(df_mtx)
cl_metrics(df_mtx,clusters)
labels = clusters.labels_
df["cluster_id"] = labels
# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters: %d' % n_clusters_)
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(df_mtx, labels))
return df, metrics.silhouette_score(df_mtx, labels)
def custom_affinity(M):
return np.array([[custom_distance_function(o,d) for o in M] for d in M])
def custom_distance_function(o, d):
"""
24 'totals',
25 'Start_Lon_mean', 'Start_Lat_mean',
27 'End_Lon_mean', 'End_Lat_mean',
29 'Start_Lon_std', 'Start_Lat_std',
31 'End_Lon_std', 'End_Lat_std',
33 'Start_Lon_meanAM', 'Start_Lat_meanAM',
35 'End_Lon_meanAM','End_Lat_meanAM',
37 'Start_Lon_stdAM', 'Start_Lat_stdAM',
39 'End_Lon_stdAM', 'End_Lat_stdAM',
41 'Start_Lon_meanPM', 'Start_Lat_meanPM',
43 'End_Lon_meanPM', 'End_Lat_meanPM',
45 'Start_Lon_stdPM', 'Start_Lat_stdPM',
47 'End_Lon_stdPM','End_Lat_stdPM'
:param o:
:param d:
:return:
"""
x = list() # values
w = list() # weights
x.append(pearsonr(o[:24], d[:24])[0]) # r2 between time profiles
w.append(0) # weight
x.append(abs((o[24] - d[24])) / float(d[24])) # relative distance between totals
w.append(0) # weight
# x.append(cosine_vect(o, d))
# w.append(0.5)
x.append(cosine_vect(o, d, 35))
w.append(1)
x.append(cosine_vect(o, d, 43))
w.append(1)
# x.append(day_type_distance(o[49], d[49]))
# w.append(0.0)
# # Start AM Lon Lat
# x.append(geo_distance(o, d, 33))
# w.append(1.0)
#
# # End AM Lon Lat
# x.append(geo_distance(o, d, 35))
# w.append(1.0)
#
# # Start PM Lon Lat
# x.append(geo_distance(o, d, 45))
# w.append(1.0)
#
# # End PM Lon Lat
# x.append(geo_distance(o, d, 47))
# w.append(1.0)module 'holidays' has no attribute 'UnitedStates'
return np.dot(x, w)
def cosine_vect(o, d, index):
# compute cosine similarity between two vectors
deltaLonAM = o[index] - o[index-2]
deltaLatAM = o[index+1] - o[index-1]
day1_vect = [deltaLonAM, deltaLatAM]
deltaLonAM = d[index] - d[index-2]
deltaLatAM = d[index+1] - d[index-1]
day2_vect = [deltaLonAM, deltaLatAM]
return cosine(day1_vect, day2_vect)
def make_clusters(_n=6):
# read
df = pd.read_csv(DAYS_PATH_IN)
df = df.set_index(df['index'])
df.drop('2018-03-21', axis = 0, inplace=True)
df.drop('2018-03-22', axis = 0, inplace = True)
df.fillna(0, inplace = True)
del df['index']
null_columns = df.columns[df.isnull().any()]
#print(null_columns)
#print(df[df.isnull().any(axis=1)][null_columns].head())
for null_column in null_columns:
print("Missing value of \t{}\t on \t{}".format(null_column,df[df[null_column].isnull()].index.values[0]))
# cluster
# for i in range(3,9):
# days = cluster_agglomeration(df, i)[0]
# quit()
days = cluster_agglomeration(df, _n)[0]
days['holidays'] = make_holidays(days)
# save
days.to_csv(CLUSTERED_DAYS_PATH_OUT)
cls = days.cluster_id.unique()
print(cls)
return days