0% found this document useful (0 votes)
2 views2 pages

Kmeans Clustering Code

The document outlines a Python script for performing K-Means clustering on the Iris dataset. It includes data importation, the elbow method for determining the optimal number of clusters, and visualization of the resulting clusters and their centroids. The script uses libraries such as NumPy, Pandas, and Matplotlib for data manipulation and plotting.

Uploaded by

Dr Sumathy V
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)
2 views2 pages

Kmeans Clustering Code

The document outlines a Python script for performing K-Means clustering on the Iris dataset. It includes data importation, the elbow method for determining the optimal number of clusters, and visualization of the resulting clusters and their centroids. The script uses libraries such as NumPy, Pandas, and Matplotlib for data manipulation and plotting.

Uploaded by

Dr Sumathy V
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
You are on page 1/ 2

#Importing Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot
as plt

df= pd.read_csv("/content/drive/MyDrive/Iris.csv")

#Splitting Dataset into X

x = df.iloc[:,1:-1].values

for i in range(1, 11):


kmeans = KMeans(n_clusters = i, init='kmeans++',
max_iter=300,n_init=10,random_state=0)
kmeans.fit(x)
wcss.append(kmeans.inertia_)

#Elbow Method

plt.plot(range(1, 11), wcss)


plt.title('The elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS') #within cluster sum of squares plt.show()

#K-Means Clustering

kmeans=KMeans(n_clusters=3,init='k-means++',
max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)

#Visualising the clusters


plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'blue', label =
'IrisSetosa',edgecolors='black',marker='*')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'yellow', label =
'IrisVersicolour',edgecolors='black',marker='p')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'olive', label =
'IrisVirginica',edgecolors='black',marker='P')

#Plotting the centroids of the clusters


plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c = 'purple',
lab el = 'Centroids',edgecolors='black')

plt.legend()

You might also like