0% found this document useful (0 votes)
82 views11 pages

K-Means Clustering Explained

The document discusses the K-means clustering algorithm. K-means clustering is an unsupervised machine learning algorithm that groups unlabeled data points into K number of clusters based on their similarities. It works by assigning each data point to the nearest cluster center and updating cluster centers to be the average of all points assigned to that cluster. This process repeats for a certain number of iterations to properly cluster the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views11 pages

K-Means Clustering Explained

The document discusses the K-means clustering algorithm. K-means clustering is an unsupervised machine learning algorithm that groups unlabeled data points into K number of clusters based on their similarities. It works by assigning each data point to the nearest cluster center and updating cluster centers to be the average of all points assigned to that cluster. This process repeats for a certain number of iterations to properly cluster the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

K means Clustering – Introduction

K-Means Clustering is an Unsupervised Machine Learning algorithm, which groups


the unlabeled dataset into different clusters.
K means Clustering
Unsupervised Machine Learning learning is the process of teaching a computer to
use unlabeled, unclassified data and enabling the algorithm to operate on that data
without supervision. Without any previous data training, the machine’s job in this
case is to organize unsorted data according to parallels, patterns, and variations.
The goal of clustering is to divide the population or set of data points into a number
of groups so that the data points within each group are more comparable to one
another and different from the data points within the other groups. It is essentially a
grouping of things based on how similar and different they are to one another.
We are given a data set of items, with certain features, and values for these features
(like a vector). The task is to categorize those items into groups. To achieve this, we
will use the K-means algorithm; an unsupervised learning algorithm. ‘K’ in the name
of the algorithm represents the number of groups/clusters we want to classify our
items into.
(It will help if you think of items as points in an n-dimensional space). The
algorithm will categorize the items into k groups or clusters of similarity. To
calculate that similarity, we will use the euclidean distance as a measurement.
The algorithm works as follows:
1. First, we randomly initialize k points, called means or cluster centroids.
2. We categorize each item to its closest mean and we update the mean’s
coordinates, which are the averages of the items categorized in that cluster
so far.
3. We repeat the process for a given number of iterations and at the end, we
have our clusters.
The “points” mentioned above are called means because they are the mean values of
the items categorized in them. To initialize these means, we have a lot of options. An
intuitive method is to initialize the means at random items in the data set. Another
method is to initialize the means at random values between the boundaries of the
data set (if for a feature x, the items have values in [0,3], we will initialize the means
with values for x at [0,3]).
The above algorithm in pseudocode is as follows:
Initialize k means with random values

--> For a given number of iterations:

--> Iterate through items:

--> Find the mean closest to the item by calculating


the euclidean distance of the item with each of the means

--> Assign item to mean

--> Update mean by shifting it to the average of the items


in that cluster

Import the necessary Libraries:

We are importing Numpy for statistical computations, Matplotlib to plot the graph,
and make_blobs from [Link].

import numpy as np

import [Link] as plt

from [Link] import make_blobs

Create the custom dataset with make_blobs and plot it


X,y = make_blobs(n_samples = 500,n_features = 2,centers = 3,random_state = 23)

fig = [Link](0)

[Link](True)

[Link](X[:,0],X[:,1])

[Link]()

Output:
Clustering dataset

Initialize the random centroids

 Python3
k = 3

clusters = {}

[Link](23)

for idx in range(k):

center = 2*(2*[Link](([Link][1],))-1)

points = []

cluster = {

'center' : center,

'points' : []

}
clusters[idx] = cluster

clusters

Output:
{0: {'center': array([0.06919154, 1.78785042]), 'points': []},
1: {'center': array([ 1.06183904, -0.87041662]), 'points': []},
2: {'center': array([-1.11581855, 0.74488834]), 'points': []}}
Plot the random initialize center with data points

 Python3
[Link](X[:,0],X[:,1])

[Link](True)

for i in clusters:

center = clusters[i]['center']

[Link](center[0],center[1],marker = '*',c = 'red')

[Link]()

Output:
Data points with random center

Define euclidean distance

 Python3
def distance(p1,p2):

return [Link]([Link]((p1-p2)**2))

Create the function to Assign and Update the cluster center

 Python3
#Implementing E step

def assign_clusters(X, clusters):

for idx in range([Link][0]):

dist = []

curr_x = X[idx]

for i in range(k):
dis = distance(curr_x,clusters[i]['center'])

[Link](dis)

curr_cluster = [Link](dist)

clusters[curr_cluster]['points'].append(curr_x)

return clusters

#Implementing the M-Step

def update_clusters(X, clusters):

for i in range(k):

points = [Link](clusters[i]['points'])

if [Link][0] > 0:

new_center = [Link](axis =0)

clusters[i]['center'] = new_center

clusters[i]['points'] = []

return clusters

Create the function to Predict the cluster for the datapoints

 Python3
def pred_cluster(X, clusters):

pred = []

for i in range([Link][0]):

dist = []

for j in range(k):

[Link](distance(X[i],clusters[j]['center']))

[Link]([Link](dist))

return pred

Assign, Update, and predict the cluster center

 Python3
clusters = assign_clusters(X,clusters)
clusters = update_clusters(X,clusters)

pred = pred_cluster(X,clusters)

Plot the data points with their predicted cluster center

 Python3
[Link](X[:,0],X[:,1],c = pred)

for i in clusters:

center = clusters[i]['center']

[Link](center[0],center[1],marker = '^',c = 'red')

[Link]()

Output:

K-means Clustering

Example 2:

Import the necessary libraries


 Python3
import pandas as pd

import numpy as np

import seaborn as sns

import [Link] as plt

import [Link] as cm

from [Link] import load_iris

from [Link] import KMeans

Load the Dataset

 Python3
X, y = load_iris(return_X_y=True)

Elbow Method
Finding the ideal number of groups to divide the data into is a basic stage in any
unsupervised algorithm. One of the most common techniques for figuring out this
ideal value of k is the elbow approach.

 Python3
#Find optimum number of cluster

sse = [] #SUM OF SQUARED ERROR

for k in range(1,11):

km = KMeans(n_clusters=k, random_state=2)

[Link](X)

[Link](km.inertia_)

Plot the Elbow graph to find the optimum number of cluster

 Python3
sns.set_style("whitegrid")

g=[Link](x=range(1,11), y=sse)

[Link](xlabel ="Number of cluster (k)",


ylabel = "Sum Squared Error",

title ='Elbow Method')

[Link]()

Output:

Elbow Method

From the above graph, we can observe that at k=2 and k=3 elbow-like situation. So,
we are considering K=3
Build the Kmeans clustering model

 Python3
kmeans = KMeans(n_clusters = 3, random_state = 2)

[Link](X)

Output:
KMeans
KMeans(n_clusters=3, random_state=2)
Find the cluster center

 Python3
kmeans.cluster_centers_

Output:
array([[5.006 , 3.428 , 1.462 , 0.246 ],
[5.9016129 , 2.7483871 , 4.39354839, 1.43387097],
[6.85 , 3.07368421, 5.74210526, 2.07105263]])
Predict the cluster group:

 Python3
pred = kmeans.fit_predict(X)

pred

Output:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2,
2, 2,
2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2,
2, 2,
2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1],
dtype=int32)
Plot the cluster center with data points

 Python3
[Link](figsize=(12,5))

[Link](1,2,1)
[Link](X[:,0],X[:,1],c = pred, cmap=[Link])

[Link](True)

for center in kmeans.cluster_centers_:

center = center[:2]

[Link](center[0],center[1],marker = '^',c = 'red')

[Link]("petal length (cm)")

[Link]("petal width (cm)")

[Link](1,2,2)

[Link](X[:,2],X[:,3],c = pred, cmap=[Link])

[Link](True)

for center in kmeans.cluster_centers_:

center = center[2:4]

[Link](center[0],center[1],marker = '^',c = 'red')

[Link]("sepal length (cm)")

[Link]("sepal width (cm)")

[Link]()

Output:

K-means clustering

Common questions

Powered by AI

Challenges from using K-means for clustering non-globular data distributions include the assumption of spherical clusters and equal cluster sizes, which can lead to poor separation and inaccurate clustering for non-globular or variably-sized clusters. These challenges can be addressed by considering alternative algorithms like DBSCAN or Gaussian Mixture Models that do not assume spherical clusters or by transforming data to fit these assumptions better .

In K-means clustering, Euclidean distance is used to measure the similarity between data points and cluster centroids. This distance determines to which cluster a data point belongs by calculating the distance between the data point and each cluster centroid and assigning the data point to the cluster with the nearest centroid. This process ensures that data points are grouped based on their proximity to cluster centers .

Re-running K-means clustering multiple times with different centroid initializations is important as it helps mitigate the impact of poor random initializations, which can lead to suboptimal clustering results. Different initializations can affect the convergence and the resulting clusters. By averaging the results or choosing the best outcome from multiple runs, one can achieve more robust and reliable clustering performance .

The primary objective of K-means clustering is to partition the dataset into k groups or clusters, where each data point belongs to the cluster with the nearest mean, effectively grouping similar data points together while ensuring that data points in different clusters are dissimilar. This is achieved through the iterative process of assigning data points to the closest mean and updating the means to the average of the assigned data points .

The elbow method determines the optimal number of clusters by plotting the sum of squared errors (SSE) for different values of k and looking for a point where the rate of decrease sharply changes, forming an elbow-like shape. At this point, the addition of more clusters no longer provides substantial improvement in the clustering outcome. This method is effective because it balances sufficient complexity to capture the data structure while avoiding unnecessary complexity that only marginally improves data fit .

Plotting clusters and their centers can validate the results of the K-means algorithm by visually confirming the separation and coherence of clusters. Visualization allows for assessment of how well centroids represent their clusters and if the data points are adequately and distinctly grouped according to expected patterns, thereby indicating the algorithm's effectiveness. It can reveal potential issues such as overlapping clusters or mis-classified data points .

The assign_clusters function in the K-means process assigns each data point to the closest centroid based on Euclidean distance, thereby defining the cluster membership for each point. The update_clusters function recalculates the position of each centroid as the mean of all data points currently assigned to it, ensuring that cluster centers reflect the current data distribution. This iterative process is repeated until convergence .

The KMeans object in sklearn handles clustering by initializing k cluster centroids, assigning each data point to the nearest centroid, updating centroids as the mean of the points in a cluster, and repeating these steps until convergence. After fitting a dataset, it produces cluster centroids and labels each data point with its corresponding cluster, allowing for the prediction and analysis of cluster membership for the given dataset .

Initialization strategies for centroids significantly affect K-means clustering as they influence the convergence speed and the quality of the final clusters. Random initialization can lead to suboptimal clusters or slow convergence, whereas methods like K-means++ help in avoiding poor initializations by choosing dispersed centroids, thus often leading to better and faster convergence. Different strategies can affect the direction and outcome of the clustering process .

The make_blobs function aids in simulating datasets for clustering algorithms by generating isotropic Gaussian blobs for clustering, allowing control over parameters such as the number of samples, features, centers, and cluster standard deviation. This helps create synthetic datasets of varying complexity which can be used to test and validate clustering algorithms like K-means under controlled conditions .

You might also like