Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- from sklearn.cluster import KMeans
- from sklearn.datasets import make_blobs
- X, y_true = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
- kmeans = KMeans(n_clusters=4, init='k-means++', n_init='auto', random_state=42)
- labels = kmeans.fit_predict(X)
- plt.figure(figsize=(8, 6))
- plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=40, edgecolor='k', alpha=0.8)
- plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],
- s=250, c='red', marker='*', label='Centroids', edgecolor='black')
- plt.title("K-Means Clustering Execution")
- plt.legend()
- plt.grid(True, linestyle='--', alpha=0.5)
- plt.show()
- ----------------------------------------------------------------------------------------
- import matplotlib.pyplot as plt
- from sklearn.datasets import make_blobs
- from sklearn.cluster import BisectingKMeans
- X, _ = make_blobs(n_samples=300, centers=1, cluster_std=1.5, random_state=42)
- bkm = BisectingKMeans(n_clusters=4, random_state=42, bisecting_strategy="biggest_inertia")
- labels = bkm.fit_predict(X)
- plt.figure(figsize=(8, 6))
- plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=30, edgecolor='k')
- plt.scatter(bkm.cluster_centers_[:, 0], bkm.cluster_centers_[:, 1], s=200, c='red', marker='X', label='Centroids')
- plt.title("Bisecting K-Means Clustering (scikit-learn)")
- plt.legend()
- plt.grid(True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment