This tutorial demonstrates the implementation of K-Means from Scikit Learn library.

###Importing Libraries
from sklearn.cluster import KMeans 
from sklearn import datasets

###Importing Dataset
iris = datasets.load_iris()

###Creating K-Means Clustering Model
k_means = KMeans(init = "k-means++", n_clusters = 4, n_init = 12)

###Fitting the Model
k_means.fit(iris)

###Evaluating Inertia of K-Means
print(k_means.inertia_)

###Cluster Labels and Cluster Centers
k_means_labels = k_means.labels_
k_means_cluster_centers = k_means.cluster_centers_
print(k_means_labels, k_means_cluster_centers)
    

Since, K-Means is an unsupervised machine learning model there is no training or learning process. As a consequence we also don’t need to create train test splits from the data since there won’t be any training or testing in that sense.

This tutorial provides the most basic elements for a K-Means implementation, so anyone can try implementing K-Means immediately.

Here you can find a slightly more detailed version of this implementation: Step by Step K-Means.

Optimization can be a crucial process determining the success of a K-Means implementation. You can read more about that here: K-Means Optimization.