-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkMeans.py
33 lines (24 loc) · 1.08 KB
/
kMeans.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
# Importing required modules
import numpy as np
from scipy.spatial.distance import cdist
# Function to implement steps given in previous section
def kmeans(x, k, no_of_iterations):
idx = np.random.choice(len(x), k, replace=False)
# Randomly choosing Centroids
centroids = x[idx, :] # Step 1
# finding the distance between centroids and all the data points
distances = cdist(x, centroids, 'euclidean') # Step 2
# Centroid with the minimum Distance
points = np.array([np.argmin(i) for i in distances]) # Step 3
# Repeating the above steps for a defined number of iterations
# Step 4
for _ in range(no_of_iterations):
centroids = []
for idx in range(k):
# Updating Centroids by taking mean of Cluster it belongs to
temp_cent = x[points == idx].mean(axis=0)
centroids.append(temp_cent)
centroids = np.vstack(centroids) # Updated Centroids
distances = cdist(x, centroids, 'euclidean')
points = np.array([np.argmin(i) for i in distances])
return points