-
Notifications
You must be signed in to change notification settings - Fork 3
/
poincarekmeans.py
97 lines (79 loc) · 3.9 KB
/
poincarekmeans.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
from random import sample
import numpy as np
## K-Means in the Poincare Disk model
class PoincareKMeans(object):
def __init__(self,n_clusters=8,n_init=20,max_iter=300,tol=1e-8,verbose=True):
self.n_clusters = n_clusters
self.n_init = n_init
self.max_iter = max_iter
self.tol = tol
self.verbose = verbose
self.labels_=None
self.cluster_centers_ = None
def fit(self,X):
n_samples = X.shape[0]
self.inertia = None
for run_it in range(self.n_init):
centroids = X[sample(range(n_samples),self.n_clusters),:]
for it in range(self.max_iter):
distances = self._get_distances_to_clusters(X,centroids)
labels = np.argmin(distances,axis=1)
new_centroids = np.zeros((self.n_clusters,2))
for i in range(self.n_clusters):
indices = np.where(labels==i)[0]
if len(indices)>0:
new_centroids[i,:] = self._hyperbolic_centroid(X[indices,:])
else:
new_centroids[i,:] = X[sample(range(n_samples),1),:]
m = np.ravel(centroids-new_centroids, order='K')
diff = np.dot(m,m)
centroids = new_centroids.copy()
if(diff<self.tol):
break
distances = self._get_distances_to_clusters(X,centroids)
labels = np.argmin(distances,axis=1)
inertia = np.sum([np.sum(distances[np.where(labels==i)[0],i]**2) for i in range(self.n_clusters)])
if (self.inertia == None) or (inertia<self.inertia):
self.inertia = inertia
self.labels_ = labels.copy()
self.cluster_centers_ = centroids.copy()
if self.verbose:
print("Iteration: {} - Best Inertia: {}".format(run_it,self.inertia))
def fit_predict(self,X):
self.fit(X)
return self.labels_
def fit_transform(self,X):
self.fit(X)
return self.transform(X)
def predict(self,X):
distances = self.transform(X)
return np.argmin(distances,axis=1)
def transform(self,X):
return _get_distances_to_clusters(X,self.cluster_centers_)
def _get_distances_to_clusters(self,X,clusters):
n_samples,n_clusters = X.shape[0],clusters.shape[0]
distances = np.zeros((n_samples,n_clusters))
for i in range(n_clusters):
centroid = np.tile(clusters[i,:],(n_samples,1))
den1 = 1-np.linalg.norm(X,axis=1)**2
den2 = 1-np.linalg.norm(centroid,axis=1)**2
the_num = np.linalg.norm(X-centroid,axis=1)**2
distances[:,i] = np.arccosh(1+2*the_num/(den1*den2))
return distances
def _poinc_to_minsk(self,points):
minsk_points = np.zeros((points.shape[0],3))
minsk_points[:,0] = np.apply_along_axis(arr=points,axis=1,func1d=lambda v: 2*v[0]/(1-v[0]**2-v[1]**2))
minsk_points[:,1] = np.apply_along_axis(arr=points,axis=1,func1d=lambda v: 2*v[1]/(1-v[0]**2-v[1]**2))
minsk_points[:,2] = np.apply_along_axis(arr=points,axis=1,func1d=lambda v: (1+v[0]**2+v[1]**2)/(1-v[0]**2-v[1]**2))
return minsk_points
def _minsk_to_poinc(self,points):
poinc_points = np.zeros((points.shape[0],2))
poinc_points[:,0] = points[:,0]/(1+points[:,2])
poinc_points[:,1] = points[:,1]/(1+points[:,2])
return poinc_points
def _hyperbolic_centroid(self,points):
minsk_points = self._poinc_to_minsk(points)
minsk_centroid = np.mean(minsk_points,axis=0)
normalizer = np.sqrt(np.abs(minsk_centroid[0]**2+minsk_centroid[1]**2-minsk_centroid[2]**2))
minsk_centroid = minsk_centroid/normalizer
return self._minsk_to_poinc(minsk_centroid.reshape((1,3)))[0]