Created
February 21, 2019 12:23
-
-
Save spikar/4264c8c32ef92d89dacc6e53ae5fb38b to your computer and use it in GitHub Desktop.
k-means implementation in python from scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class K_Means: | |
def __init__(self, k=2, tol=0.001, max_iter=300): | |
self.k = k | |
self.tol = tol | |
self.max_iter = max_iter | |
def fit(self, data): | |
self.centroids = {} | |
for i in range(self.k): | |
self.centroids[i] = data[i] | |
for i in range(self.max_iter): | |
self.classifications = {} | |
for i in range(self.k): | |
self.classifications[i] = [] | |
for featureset in data: | |
distances = [np.linalg.norm(featureset - self.centroids[centroid]) for centroid in self.centroids] | |
classification = distances.index(min(distances)) | |
self.classifications[classification].append(featureset) | |
prev_centroids = dict(self.centroids) | |
for classification in self.classifications: | |
self.centroids[classification] = np.average(self.classifications[classification], axis=0) | |
optimized = True | |
for c in self.centroids: | |
original_centroid = prev_centroids[c] | |
current_centroid = self.centroids[c] | |
if np.sum((current_centroid - original_centroid) / original_centroid * 100.0) > self.tol: | |
print(np.sum((current_centroid - original_centroid) / original_centroid * 100.0)) | |
optimized = False | |
if optimized: | |
break | |
def predict(self, data): | |
distances = [np.linalg.norm(data - self.centroids[centroid]) for centroid in self.centroids] | |
classification = distances.index(min(distances)) | |
return classification |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment