Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
889 B
1
Indexable
Never

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

"""Load the dataset"""

iris = load_iris()
X = iris.data

iris

"""Set the number of clusters"""

k = 3

"""Fit KMeans model to the data"""

kmeans = KMeans(n_clusters=k, n_init=10)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)



sa=silhouette_score(X,y_kmeans)
print("silhouette score", sa)




"""Plot the clusters"""

plt.figure(figsize=(8, 6))
for i in range(k):
    plt.scatter(X[y_kmeans == i, 0], X[y_kmeans == i, 1], s=50, label=f'Cluster {i+1}')


plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red', marker='s', label='Centroids')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.title('KMeans Clustering of Iris Dataset')
plt.legend()
plt.show()


Leave a Comment