0% found this document useful (0 votes)
5 views5 pages

9 Ds

The document outlines a practical exercise on K-means clustering using the Iris dataset. It includes data loading, exploratory data analysis with visualizations, and the implementation of the K-means algorithm to identify clusters within the data. The results are visualized through various plots, including the elbow method and 3D scatter plots of the clustered data.

Uploaded by

divypandya966
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)
5 views5 pages

9 Ds

The document outlines a practical exercise on K-means clustering using the Iris dataset. It includes data loading, exploratory data analysis with visualizations, and the implementation of the K-means algorithm to identify clusters within the data. The results are visualized through various plots, including the elbow method and 3D scatter plots of the clustered data.

Uploaded by

divypandya966
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/ 5

BTECH-IT-A2 DATA SCIENCE IU2341221247

Practical-9

Aim: K-means clustering of Iris dataset.

import pandas as pd import numpy as np


import matplotlib.pyplot as plt import
seaborn as sns from sklearn.cluster
import KMeans from sklearn.metrics

import silhouette_score from sklearn.preprocessing


import MinMaxScaler iris = pd.read_csv("IRIS.csv")
x = iris.iloc[:, [0, 1, 2,3]].values iris.info()
iris[0:10]

iris_outcome = pd.crosstab(index=iris["species"], columns="count") iris_outcome

iris_setosa=iris.loc[iris["species"]=="Iris-setosa"] iris_virginica=iris.loc[iris["species"]=="Iris-
virginica"] iris_versicolor=iris.loc[iris["species"]=="Iris-versicolor"]
BTECH-IT-A2 DATA SCIENCE IU2341221247

sns.boxplot(x="species",y="petal_length",data=iris)
plt.show()

sns.violinplot(x="species",y="petal_length",data=iris)
plt.show()
BTECH-IT-A2 DATA SCIENCE IU2341221247

sns.set_style("whitegrid")
sns.pairplot(iris,hue="species",size=3); plt.show()

from sklearn.cluster import KMeans

wcss = [] for i in range(1, 11):

kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10,


random_state = 0)
kmeans.fit(x)
wcss.append(kmeans.inertia_) plt.plot(range(1, 11),

wcss) plt.title('The elbow method')


BTECH-IT-A2 DATA SCIENCE IU2341221247

plt.xlabel('Number of clusters') plt.ylabel('WCSS')

#within cluster sum of squares plt.show()

kmeans
= KMeans(n_clusters = 3, init = 'k-means++', max_iter = 300, n_init = 10,
random_state = 0) y_kmeans = kmeans.fit_predict(x) plt.scatter(x[y_kmeans == 0,
0], x[y_kmeans == 0, 1], s = 100, c = 'purple', label =
'Iris-setosa') plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c =
'orange', label =
'Iris-versicolour')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Iris-
virginica')

#Plotting the centroids of the clusters


plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c = 'red', label
= 'Centroids') plt.legend()
BTECH-IT-A2 DATA SCIENCE IU2341221247

fig = plt.figure(figsize = (15,15)) ax = fig.add_subplot(111, projection='3d')


plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100,
c = 'purple', label ='Iris-setosa')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c
= 'orange', label ='Iris-versicolour')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c
= 'green', label ='Iris-virginica')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c
= 'red',
label= 'Centroids') plt.show()

You might also like