-
-
Save MoGaber/6b418f1287f0d0bb6d2084a39ff190c9 to your computer and use it in GitHub Desktop.
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
def scratch_k_means(k, all_data): | |
centroids= [(random.randint(min(x_data), max(x_data)), random.randint(min(y_data), max(y_data))) for i in range(k)] | |
for i in range(100): | |
distances = np.concatenate([np.linalg.norm(all_data - centroid, axis=1).reshape(-1, 1) for centroid in centroids],axis=1) | |
labels = np.argmin(distances, axis=1) | |
centroids =pd.DataFrame(np.concatenate([all_data, labels.reshape(-1, 1)], axis=1)).groupby(2).mean().values | |
return centroids, labels |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment