Ex No: Date: K-Means Clustering Using Python: Scatter
Ex No: Date: K-Means Clustering Using Python: Scatter
DATE:
Aim:
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:
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:
plt.scatter(x, y)
plt.show()
[(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_)
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.