Skip to content

Instantly share code, notes, and snippets.

@MoGaber
Created April 25, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoGaber/6b418f1287f0d0bb6d2084a39ff190c9 to your computer and use it in GitHub Desktop.
Save MoGaber/6b418f1287f0d0bb6d2084a39ff190c9 to your computer and use it in GitHub Desktop.
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