Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created September 17, 2018 16:50
Show Gist options
  • Select an option

  • Save ImadDabbura/5a4416ece0e101eb48d7707b6df4e2f8 to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/5a4416ece0e101eb48d7707b6df4e2f8 to your computer and use it in GitHub Desktop.
n_iter = 9
fig, ax = plt.subplots(3, 3, figsize=(16, 16))
ax = np.ravel(ax)
centers = []
for i in range(n_iter):
# Run local implementation of kmeans
km = Kmeans(n_clusters=2,
max_iter=3,
random_state=np.random.randint(0, 1000, size=1))
km.fit(X_std)
centroids = km.centroids
centers.append(centroids)
ax[i].scatter(X_std[km.labels == 0, 0], X_std[km.labels == 0, 1],
c='green', label='cluster 1')
ax[i].scatter(X_std[km.labels == 1, 0], X_std[km.labels == 1, 1],
c='blue', label='cluster 2')
ax[i].scatter(centroids[:, 0], centroids[:, 1],
c='r', marker='*', s=300, label='centroid')
ax[i].set_xlim([-2, 2])
ax[i].set_ylim([-2, 2])
ax[i].legend(loc='lower right')
ax[i].set_title(f'{km.error:.4f}')
ax[i].set_aspect('equal')
plt.tight_layout();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment