0% found this document useful (0 votes)
49 views10 pages

Ex No: Date: K-Means Clustering Using Python: Scatter

This document discusses implementing K-means clustering using Python. K-means clustering groups unlabeled data points into K number of clusters. It imports libraries like matplotlib and sklearn for plotting, preprocessing, metrics, and clustering algorithms. It performs K-means clustering on a sample dataset with x and y coordinates by calculating inertia for different values of K and plotting elbow curve to find optimal K. Finally, it plots the clustered data based on the predicted labels.

Uploaded by

Jasmitha B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

Ex No: Date: K-Means Clustering Using Python: Scatter

This document discusses implementing K-means clustering using Python. K-means clustering groups unlabeled data points into K number of clusters. It imports libraries like matplotlib and sklearn for plotting, preprocessing, metrics, and clustering algorithms. It performs K-means clustering on a sample dataset with x and y coordinates by calculating inertia for different values of K and plotting elbow curve to find optimal K. Finally, it plots the clustered data based on the predicted labels.

Uploaded by

Jasmitha B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Ex No: K-MEANS CLUSTERING USING PYTHON

DATE:

Aim:

To implement K-means clustering classification using Python programming.

Description:
K-means clustering:

K-Means Clustering is an Unsupervised Learning algorithm, which groups the unlabeled dataset into
different clusters. Here K defines the number of pre-defined clusters that need to be created in the
process, as if K=2, there will be two clusters, and for K=3, there will be three clusters, and so on.

Pyplot:
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the
plt alias:

import matplotlib.pyplot as plt scatter():

The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for
the values of the x-axis, and one for values on the y-axis
sklearn.
preprocessing:

The sklearn. preprocessing package provides several common utility functions and transformer classes
to change raw feature vectors into a representation that is more suitable for the downstream estimators.

Sklearn. metrics:
The sklearn. metrics module implements several loss, score, and utility functions to measure
classification performance. Some metrics might require probability estimates of the positive class,
confidence values, or binary decision values.

Sklearn. model_selection:

Model_selection is a method for setting a blueprint to analyze data and then using it to measure new
data. Selecting a proper model allows you to generate accurate results when making a prediction.

Sklearn. cluster:

It stands for “Density-based spatial clustering of applications with noise”. This algorithm is based on
the intuitive notion of “clusters” & “noise” that clusters are dense regions of the lower density in the
data space, separated by lower density regions of data points. Scikit-learn have sklearn. cluster.

Methods:

IMPLEMENTATION:

import matplotlib.pyplot as plt

x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]


y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]

plt.scatter(x, y)
plt.show()

from sklearn.cluster import KMeans

data = list(zip(x, y))


inertias = []
data

[(4, 21), (5, 19), (10, 24), (4, 17), (3, 16), (11, 25), (14, 24), (6, 22), (10, 21), (12, 21)]

kmeans = KMeans(n_clusters=1)
kmeans.fit(data)
print(kmeans.inertia_)

218.9

kmeans = KMeans(n_clusters=2)
kmeans.fit(data)
print(kmeans.inertia_)

56.4

kmeans = KMeans(n_clusters=3)
kmeans.fit(data)
print(kmeans.inertia_)

32.86666666666667
kmeans = KMeans(n_clusters=4)
kmeans.fit(data)
print(kmeans.inertia_)

19.0

for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)

plt.plot(range(1,11), inertias, marker='o')


plt.title('Elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()

kmeans = KMeans(n_clusters=2)
kmeans.fit(data)

plt.scatter(x, y, c=kmeans.labels_)
plt.show()
Problem Implementation Time Viva Total
Understanding Management

RESULT:

Thus the K -Means clustering classification using Python programming has been understood and
executed successfully.

You might also like