Skip to content

Instantly share code, notes, and snippets.

@DFoly
Created February 20, 2019 17:38
Show Gist options
  • Select an option

  • Save DFoly/1e7f87cc5359bbbc17e94d033cb5a73d to your computer and use it in GitHub Desktop.

Select an option

Save DFoly/1e7f87cc5359bbbc17e94d033cb5a73d to your computer and use it in GitHub Desktop.
plt.figure(figsize = (10,8))
from scipy.spatial.distance import cdist
def plot_kmeans(kmeans, X, n_clusters=3, rseed=0, ax=None):
labels = kmeans.fit_predict(X)
# plot the input data
ax = ax or plt.gca()
ax.axis('equal')
ax.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis', zorder=2)
# plot the representation of the KMeans model
centers = kmeans.cluster_centers_
radii = [cdist(X[labels == i], [center]).max()
for i, center in enumerate(centers)]
for c, r in zip(centers, radii):
ax.add_patch(plt.Circle(c, r, fc='#CCCCCC', lw=3, alpha=0.5, zorder=1))
plot_kmeans(kmeans, Y_sklearn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment