df = seaborn.load_dataset("iris")
df_features = df.drop(labels=["species"], axis=1)
Now, we are using k-means clustering with 3 clusters. The random_state=1 parameter controls the random number generator that is used to randomly select the cluster centroids initially (How does k-means clustering work?)
The fit_predict() method learns from the dataset, calculates the centroids, and calculates the labels for each sample based on the nearest centroid to which the sample belongs.
kmeans = KMeans(n_clusters=3, random_state=1) kmeans.fit_predict(df_features)
Now, we are calculating the average silhouette score over all the samples.
score = silhouette_score(df_features, kmeans.labels_)
The output of the above program will be:
Silhouette Score: 0.5528190123564102








































0 Comments