note that in this kind of algorithmonly the raw uncategorized data is fed to the algorithm it is up to the algorithm to find out if the data has inherent groups within it to understand how this algorithm workswe will examine data points consisting of and values we will feed these values to the learning algorithm and expect that the algorithm will cluster the data into two sets we will color the two sets so that the clusters are visible let' create sample data of records of and pairsimport numpy as np import matplotlib pyplot as plt original_set - np random rand( second_set np random rand( original_set[ :second_set firstwe create records with - np random rand( in each of the recordswe will use the data in it to represent and values that will eventually be plotted the last numbers in original_set will be replaced by np random rand( in effectwhat we have done is to create two subsets of datawhere one set has numbers in the negative while the other set has numbers in the positive it is now the responsibility of the algorithm to discover these segments appropriately we instantiate the kmeans algorithm class and pass it n_clusters= that makes the algorithm cluster all its data under only two groups it is through series of trial and error that this figure is obtained but for academic purposeswe already know this number it is not at all obvious when working with unfamiliar datasets from the real worldfrom sklearn cluster import kmeans kmean kmeans(n_clusters= kmean fit(original_setprint(kmean cluster_centers_print(kmean labels_the dataset is passed to the fit function of kmeankmean fit(original_setthe clusters generated by the algorithm will revolve around certain mean point the points that define these two mean points are obtained by kmean cluster_centers_