-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.py
59 lines (51 loc) · 1.74 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
import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
import math
points = np.array([[160 180],
[ 40 , 0],
[100 , 20],
[140 , 0],
[180 ,120],
[100 ,140],
[ 80 ,100],
[120 ,40],
[197 ,197]])
def distance(origin, destination): #found here https://gist.github.com/rochacbruno/2883505
lat1, lon1 = origin[0],origin[1]
lat2, lon2 = destination[0],destination[1]
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def create_clusters(number_of_clusters,points):
kmeans = KMeans(n_clusters=number_of_clusters, random_state=0).fit(points)
l_array = np.array([[label] for label in kmeans.labels_])
clusters = np.append(points,l_array,axis=1)
return clusters
def validate_solution(max_dist,clusters):
_, __, n_clust = clusters.max(axis=0)
n_clust = int(n_clust)
for i in range(n_clust):
two_d_cluster=clusters[clusters[:,2] == i][:,np.array([True, True, False])]
if not validate_cluster(max_dist,two_d_cluster):
return False
else:
continue
return True
def validate_cluster(max_dist,cluster):
distances = cdist(cluster,cluster, lambda ori,des: int(round(distance(ori,des))))
print(distances)
print(30*'-')
for item in distances.flatten():
if item > max_dist:
return False
return True
if __name__ == '__main__':
for i in range(2,len(points)):
print(i)
print(validate_solution(20,create_clusters(i,points)))