<jupyter_start><jupyter_text>CLUSTERING IN IRIS DATA WITH K-MEANS  First we will find the K Value with the ELBOW MethodK Value calculation means finding the optimal count of clusters.<jupyter_code>clusters = range(1, 6)
WCSS = []
for k in clusters:
    #we will create the KMeans model
    model = KMeans(n_clusters=k)
    #now we will fit the model to iris data
    model.fit(iris)
    #generate inertia and append those values to WCSS
    WCSS.append(model.inertia_)
    
    
    # Now we will plot our clusters and the inertia
    
plt.plot(clusters, WCSS, "-o")
plt.xlabel("No of Clusters")
plt.ylabel("WCSS")
plt.title("Elbow Method")
plt.xticks(clusters)
plt.show()   
    
# So based on above plot we should create 3 clusters for our model .
import numpy as np
import matplotlib.pyplot as plt # For plotting
from sklearn.cluster import KMeans # For Clustering
import pandas as pd
from sklearn.datasets import load_iris

irisdata = load_iris()
iris = pd.DataFrame(irisdata.data)
iris.head()
# Column Names
irisdata.feature_names
# Col Names into the IRIS Dataframe
iris.columns = irisdata.feature_names
iris.head()
# Setosa, Versicolor and Virginica:: clusters 03
# dtree = DecisionTree()
model = KMeans(n_clusters = 3)

model
# Fit the K Means Clustering on my IRIS DATA
model.fit(iris)
# Predict the Labels
labels = model.predict(iris)
labels
# Plotting the IRIS Data and the Cluster Centroid too...
# Lets Find out the Centroids
centroid = model.cluster_centers_
centroid
centroid_x = centroid[: , 0]
centroid_y = centroid[:, 1]
centroid_x  
xs = irisdata.data[: , 0]
ys = irisdata.data[: , 1]

#lets plot these together

import matplotlib.patches as mpatches

plt.figure(figsize=(20,10))
plt.scatter(xs , ys , c = labels , s=60 )
plt.scatter(centroid_x, centroid_y , marker = "D" , c="r" , s = 120)
plt.xlabel("Sepal Length in cms" , size = 20)
plt.ylabel("Sepal Width in cms" , size = 20)
plt.title("Clustered IRIS data " , size = 30)

one = mpatches.Patch(facecolor = 'purple', label='Setosa', linewidth = 1, edgecolor = 'black')
two = mpatches.Patch(facecolor = 'teal', label = 'Versicolor', linewidth = 1, edgecolor = 'black')
three = mpatches.Patch(facecolor = 'yellow', label = "Virginica", linewidth = 1, edgecolor = 'black')

legend = plt.legend(handles=[one, two, three],loc = 4, fontsize = 'xx-large',fancybox= True,borderpad=2,labelspacing=2)

plt.show()<jupyter_output><empty_output><jupyter_text>So we have 3 clusters corresponding to 3 different species of flowers , i.e, Setosa , Virginica and Versicolor.<jupyter_code>centroid_pl = centroid[:,2]
centroid_pw = centroid[:,3]
centroid_pl
axis_pl = irisdata.data[:,2]
axis_pw = irisdata.data[:,3]
axis_pl
import matplotlib.patches as mpatches

plt.figure(figsize=(20,10))
plt.scatter(axis_pl , axis_pw ,c =labels , s=80)
plt.scatter(centroid_pl , centroid_pw , marker = "D" , c = "r" , s=120)

plt.xlabel("Petal Length in cms" , size = 20)
plt.ylabel("Petal Width in cms" , size = 20)
plt.title("Clustering 3 species of flowers in IRIS data " , size = 30)

handle1 = mpatches.Patch(facecolor = "purple" , label ="Setosa" , linewidth = 1, edgecolor = 'black')
handle2 = mpatches.Patch(facecolor = "teal" , label ="Versicolor", linewidth = 1, edgecolor = 'black')
handle3 = mpatches.Patch(facecolor = "yellow" , label ="Virginica", linewidth = 1, edgecolor = 'black')



legend = plt.legend(handles=[handle1, handle2, handle3],loc=4, fontsize = "xx-large" ,borderpad= 2 , labelspacing=2 )

plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup