-
Notifications
You must be signed in to change notification settings - Fork 0
/
K_means_clustering.py
53 lines (36 loc) · 1.7 KB
/
K_means_clustering.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
# This program defines a KMeans class that represents
# the K-means clustering algorithm. The fit method fits
# the model to the data and finds the optimal centroids,
# while the predict method assigns each point to the closest
# centroid based on Euclidean distance. The program also includes
# an example usage that demonstrates how to use the KMeans class on a sample dataset and visualize the results using matplotlib.
import numpy as np
import matplotlib.pyplot as plt
class KMeans:
def __init__(self, k=2, max_iters=100):
self.k = k
self.max_iters = max_iters
def fit(self, X):
self.centroids = X[np.random.choice(range(len(X)), self.k, replace=False)]
for _ in range(self.max_iters):
clusters = [[] for _ in range(self.k)]
for point in X:
distances = [np.linalg.norm(point - centroid) for centroid in self.centroids]
closest_centroid = np.argmin(distances)
clusters[closest_centroid].append(point)
prev_centroids = self.centroids.copy()
for i in range(self.k):
if clusters[i]:
self.centroids[i] = np.mean(clusters[i], axis=0)
if np.all(prev_centroids == self.centroids):
break
def predict(self, X):
return [np.argmin([np.linalg.norm(x - centroid) for centroid in self.centroids]) for x in X]
# Example usage
X = np.array([[1, 2], [2, 1], [2, 4], [3, 2], [7, 2], [6, 4], [7, 3], [8, 4], [9, 5]])
kmeans = KMeans(k=2, max_iters=100)
kmeans.fit(X)
labels = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(kmeans.centroids[:, 0], kmeans.centroids[:, 1], marker='X', color='red')
plt.show()