## k means clustering algorithm python example

K Means Clustering is unsupervised learning algorithm in python (i.e.it’s tries to cluster the different data based on their similarity) and another meaning is that there is no outcome to be predicted data. K Means Clustering algorithm just tries to find patterns in the data.

There are 3 steps for K Means Clustering with Python:
• # step1: Initialisation – K initial means centroid in python are generated at random
• # step2: Assignment – K clusters are created by observation with the nearest centroids data
• # step3: Update – It’s becomes the new mean
wan to learn learn Logistic regression then click on it.

### Initialisation – K initial means centroide in python are generated at random

Import Libraries

import seaborn as ns

import matplotlib.pyplot as plt

%matplotlib inline

Create some Data

from sklearn.datasets import make_blobs

### Assignment – K clusters are created by observation with the nearest centroides data

data = make_blobs(n_samples=190, n_features=2,

centers=4, cluster_std=1.7,random_state=100)

Visualize Data

plt.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap=’rainbow’)

<matplotlib.collections.PathCollection at 0x11ab34d30>

 k means clustering algorithm python example

from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=4)

kmeans.fit(data[0])

KMeans(copy_x=True, init=’k-means++’, max_iter=200, n_clusters=4, n_init=9,

n_jobs=1, precompute_distances=’auto’, random_state=None, tol=0.001,

verbose=0)

#### Update – It’s becomes the new mean

kmeans.cluster_centers_

array([[-3.13591321,  6.95389851],

[-7.46941837, -5.56081545],

[-1.0123077 ,  3.13407664],

[ 2.71749226,  6.01388735]])

kmeans.labels_

array([1, 3, 1, 3, 3, 0, 2, 1, 3, 1, 2, 1, 3, 3, 2, 1, 3, 1, 0, 2, 2, 1, 1,

1, 1, 0, 0, 1, 3, 3, 2, 0, 3, 1, 1, 2, 0, 1, 0, 1, 0, 2, 2, 1, 1, 1,

1, 1, 0, 1, 1, 2, 3, 1, 0, 2, 1, 1, 2, 3, 0, 3, 0, 2, 3, 1, 0, 3, 1,

3, 3, 1, 0, 1, 0, 3, 3, 1, 2, 1, 1, 0, 3, 0, 1, 1, 1, 2, 1, 0, 0, 1,

3, 1, 1, 0, 3, 2, 0, 3, 1, 0, 1, 1, 3, 1, 0, 3, 0, 0, 3, 2, 2, 3, 2,

2, 2, 2, 3, 2, 1, 2, 1, 2, 1, 3, 2, 1, 0, 2, 2, 2, 1, 0, 0, 2, 3, 2,

2, 1, 0, 3, 0, 2, 2, 3, 1, 0, 2, 2, 2, 2, 1, 3, 1, 2, 3, 3, 3, 1, 1,

2, 1, 2, 0, 2, 1, 3, 2, 1, 3, 1, 2, 3, 1, 2, 3, 3, 0, 3, 2, 0, 0, 1,

1, 0, 3, 0, 0, 1, 3, 3, 3, 2, 0, 1, 3, 3, 0, 3], dtype=int32)

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True,figsize=(11,7))

ax1.set_title(‘K Means cluster’)

ax1.scatter(data[0][:,0],data[0][:,1],c=kmeans.labels_,cmap=’rainbow’)

ax2.set_title(“Original/main”)

ax2.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap=’rainbow’)

<matplotlib.collections.PathCollection at 0x11da01be0>

 k means clustering examples

Summary:
In this section we learn the k means clustering algorithm python example in detailed, about this section if you have any problem then please comment me.
Tags:
k means clustering algorithm python example, python, machine learning

If you want to learn sector support machine algorithms then click on it.
BEST OF LUCK!!!

### sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.