Skip to content

Instantly share code, notes, and snippets.

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

  • Save ImadDabbura/1ad5d599b27d23efae5d284081b531ac to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/1ad5d599b27d23efae5d284081b531ac to your computer and use it in GitHub Desktop.
# Standardize the data
X_std = StandardScaler().fit_transform(df)
# Run local implementation of kmeans
km = Kmeans(n_clusters=2, max_iter=100)
km.fit(X_std)
centroids = km.centroids
# Plot the clustered data
fig, ax = plt.subplots(figsize=(6, 6))
plt.scatter(X_std[km.labels == 0, 0], X_std[km.labels == 0, 1],
c='green', label='cluster 1')
plt.scatter(X_std[km.labels == 1, 0], X_std[km.labels == 1, 1],
c='blue', label='cluster 2')
plt.scatter(centroids[:, 0], centroids[:, 1], marker='*', s=300,
c='r', label='centroid')
plt.legend()
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.xlabel('Eruption time in mins')
plt.ylabel('Waiting time to next eruption')
plt.title('Visualization of clustered data', fontweight='bold')
ax.set_aspect('equal');
@Saravanandu

Copy link
Copy Markdown

AttributeError: 'KMeans' object has no attribute 'centroids'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment