Loading…
DS · Clustering · Program 23 · In the record
K-Means clustering.
1import matplotlib.pyplot as plt2from sklearn.datasets import make_blobs3from sklearn.cluster import KMeans4 5X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.8, random_state=42)6kmeans = KMeans(n_clusters=4, n_init=10, random_state=42)7labels = kmeans.fit_predict(X)8print("Cluster Centres:\n", kmeans.cluster_centers_)9 10plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')11plt.scatter(*kmeans.cluster_centers_.T, c='red', marker='X', s=200)12plt.title("K-Means Clustering")13plt.show()Cluster Centres: [[-2.63715917 8.98563949] [-6.84180708 -6.84038791] [ 4.70253968 2.02807134] [-8.83330596 7.21790214]]

Run program 23 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.