0% found this document useful (0 votes)
34 views

Tutorial For K Means Clustering in Python Sklearn - MLK - Machine Learning Knowledge-4

The document is a tutorial on applying K-means clustering with 5 clusters in Python Sklearn. It shows the code to fit a Kmeans model with n_clusters set to 5. The clustering results are then visualized and seen to produce good results by separating the data into 5 distinct clusters. The tutorial concludes by explaining it has shown how to implement K-means clustering in Sklearn to achieve customer segmentation.

Uploaded by

jefferyleclerc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Tutorial For K Means Clustering in Python Sklearn - MLK - Machine Learning Knowledge-4

The document is a tutorial on applying K-means clustering with 5 clusters in Python Sklearn. It shows the code to fit a Kmeans model with n_clusters set to 5. The clustering results are then visualized and seen to produce good results by separating the data into 5 distinct clusters. The tutorial concludes by explaining it has shown how to implement K-means clustering in Sklearn to achieve customer segmentation.

Uploaded by

jefferyleclerc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like