3/12/24, 2:44 PM Tutorial for K Means Clustering in Python Sklearn - MLK - Machine Learning Know ledge
Applying Kmeans with 5 Clusters (K=5)
K Mean clustering is applied with SKlearn KMeans() by passing the value of k=5
In [19]:
kmeans = cluster.KMeans(n_clusters=5)
kmeans = kmeans.fit(pca_df)
On visualizing the clustering it can be seen that it has produced good results.
In [20]:
pca_df['Clusters'] = kmeans.labels_
sns.scatterplot(x="principal component 1", y="principal component
2",hue = 'Clusters', data=pca_df,palette='viridis')
Out[20]:
··· ···
https://machinelearningknow ledge.ai/tutorial-for-k-means-clustering-in-python-sklearn/ 30/35
3/12/24, 2:44 PM Tutorial for K Means Clustering in Python Sklearn - MLK - Machine Learning Know ledge
Conclusion
We hope you liked our tutorial and now better understand how to implement K-means
clustering using Sklearn(Scikit Learn) in Python. Here, we have illustrated an end-to-end
example of using a dataset to build a K-means clustering model to achieve customer
segmentation using(KMeans Clustering in Python.
References:
···
https://machinelearningknow ledge.ai/tutorial-for-k-means-clustering-in-python-sklearn/ 31/35