-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkmedoids.py
53 lines (42 loc) · 1.91 KB
/
kmedoids.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
import numpy as np
from scipy.spatial.distance import cdist
from .base_unsupervized import BaseUnsupervized
class Kmedoids(BaseUnsupervized):
'''K-Medoids clustering algorithm
Parameters
----------
k : int,
Number of centers
max_iter : int,
maximum number of iterations of the algo
'''
def __init__(self,k=3,max_iter=100):
self.k = k
self.max_iter = max_iter
self.centroids = False
def _compute_dist(self,x,y):
return cdist(x,y)
def fit(self,X):
dist_matrix = self._compute_dist(X,X)
## Init (kmeans++)
centroids = [np.random.randint(0,X.shape[0])]
for i in range(self.k-1):
prob_dist_squared = np.min(dist_matrix[centroids],axis=0)**2
prob_dist_squared = prob_dist_squared / sum(prob_dist_squared) # Normalize probability
centroids.append(int(np.random.choice(np.arange(X.shape[0]),1,p=prob_dist_squared)))
centroids = np.array(centroids)
old_centroids = np.zeros(centroids.shape)
i = 0
while i < self.max_iter and np.any(old_centroids != centroids) :
old_centroids = np.copy(centroids)
clusters = np.argmin(dist_matrix[centroids],axis=0) # Assign each point a cluster (the closest mean to the point)
# Update the means with the mean of the point in the cluster
for j in range(self.k):
cluster_point = np.where(clusters == j)[0]
inner_centroid_dist = np.sum(dist_matrix[cluster_point,cluster_point],axis=0)
centroids[j] = cluster_point[np.argmin(inner_centroid_dist)]
i+=1
self.centroids = X[centroids]
def predict(self,X):
dist_centroids = self._compute_dist(self.centroids,X) # compute all dists between the means and every point
return np.argmin(dist_centroids,axis=0)