Skip to main content

The future of AI in business and its potential to transform industries

Making Data clusters for iris dataset using k-means algorithm



Hi... In this project, we will create a machine learning model and classify data clusters for different category of flowers.

The dataset we gonna use is Iris dataset.

The category of the flower will be decided by the features (sepal length, sepal width, petal length, petal width)

But first, let's quickly recap what is k-mean.

K-means 

  • K-means comes under the category of unsupervised machine learning.
  • The k-means algorithm creates clusters of data by separating samples in n groups of equal variance, minimizing criterion known as inertia.


We will cover this project on the following points.

1. Importing all required libraries 

2. Loading and preparing data

3. Using Elbow Method

4. Creating a K-means classifier model

5. Plotting graph for Iris Dataset and observe

6. Using Principle component method (PCA)

7. Plotting graph for Classified dataset


1. Importing all required libraries

 First of all, we will import all the required modules and libraries for our project. In this project, we will use the pandas, NumPy, matplotlib and sklearn. 


code.  

import pandas as pd

import numpy as np

from matplotlib import pyplot as plt

from sklearn.datasets import load_iris

from sklearn.decomposition import PCA  

from sklearn.cluster import KMeans



2. Loading and preparing data

This section we will use the built-in iris dataset in the sklearn module. we will also observe all its features and arrange the data inside the data frame. 


code.

iris=load_iris()

iris['data'][:5]



 iris['target'][:5]


iris["feature_names"]


data=pd.DataFrame(data=np.c_[iris['data'],iris['target']], columns=iris['feature_names']+['target'])

data.head()



3. Using Elbow Method

The elbow method is used to get the optimum value of K in K-means. 

The elbow-method runs k-means clustering on the dataset for a range of values for K (say from 1-10) and from each value of  K computes an average score for all clusters.


code. 


sse=[]

k_range=range(1,10)


for k in k_range:

    kmeans=KMeans(n_clusters=k)

    kmeans.fit(iris['data'])

    sse.append(kmeans.inertia_)

plt.plot(k_range,sse)

plt.title("Elbow Method")

plt.xlabel("no. of clusters")

plt.ylabel("average distortion score")

plt.show()

In the above graph the elbow point is 3. Hence we will create 3 classified clusters.  


4. Creating a K-means classifier model  

 Now its time to create the Kmeans clustering model. For that, we will use the Kmeans model and fit the iris features and get the predicted values. 

After getting the predicted values to attach all the values to the target column of the dataset. Now collect all the data related to target=0, 1, and 2.

and finally, calculate the centroids using the cluster centre.


code  

model=KMeans(n_clusters=3)

model.fit(iris['data'])

data.target=model.labels_

df1=data[data.target==0]

df2=data[data.target==1]

df3=data[data.target==2]

model.cluster_centers_ 


5. Plotting graph for Iris Dataset and observe  

Let's plot our model prepared dataset and let see how it looks.


code

plt.figure(figsize=(12,4))

plt.subplot(1,2,1)


plt.scatter(df1[['sepal length (cm)']],df1[['sepal width (cm)']],color="red",label="setosa")

plt.scatter(df2[['sepal length (cm)']],df2[['sepal width (cm)']],color="green",label="versicolor")

plt.scatter(df3[['sepal length (cm)']],df3[['sepal width (cm)']],color="blue",label="verginica")

plt.scatter(model.cluster_centers_[:,[0]],model.cluster_centers_[:,[1]],color="cyan",label="centroid")

plt.title('sepal length vs sepal width')

plt.legend()


plt.subplot(1,2,2)


plt.scatter(df1[['petal length (cm)']],df1[['petal width (cm)']],color="red",label="setosa")

plt.scatter(df2[['petal length (cm)']],df2[['petal width (cm)']],color="green",label="versicolor")

plt.scatter(df3[['petal length (cm)']],df3[['petal width (cm)']],color="blue",label="verginica")

plt.scatter(model.cluster_centers_[:,[2]],model.cluster_centers_[:,[3]],color="cyan",label="centroid")

plt.title('petal length vs petal width')

plt.legend()



From the above graph, we can observe that the data points of the setosa and verginica are overlapping each other.
We need to fix that.

6. Using Principle component method (PCA) 

PCA is an Unsupervised, non-parametric statistical technic primarily used for dimensional reduction in machine learning.

PCA can also be used to filter the noisy dataset.


code   

pca=PCA(n_components=2)

xp=pca.fit_transform(iris['data'])

xp


newdf=pd.DataFrame(xp,columns=['principle_component1','principle_component2'])

newdf


newdf['target']=model.labels_

newdf


7. Plotting graph for Classified dataset


Finally, let's plot our prepared data.

code 

df1=newdf[newdf.target==0]
df2=newdf[newdf.target==1]
df3=newdf[newdf.target==2] 


plt.scatter(df1.principle_component1,df1.principle_component2,color="red",label="setosa",marker='>')
plt.scatter(df2.principle_component1,df2.principle_component2,color="green",label="versiocolor",marker='D')
plt.scatter(df3.principle_component1,df3.principle_component2,color="blue",label="verginica")
plt.xlabel('principle component 1')
plt.ylabel('principle component 2')
plt.title('Data clusters of setosa, versicolor and verginica')

plt.legend()



Video Demonstration




Thanks for your time and stay creative...


Popular posts from this blog

Top 7 domains to expertise after learning python

Top 7 domains to expertise after learning python Python is one of the most popular programming languages. It is an object-oriented, dynamic semantics and high-level programming language. It's high-level built-in data structure combined with dynamic binding and dynamic typing makes it attractive for rapid application development. Often programmers fall in love with python because of the increased productivity it provides. Python is one of the most readable languages in the world right now. This language is very popular among developers and is widely used by many programmers to create application and programs. The implementation of this programming language is simple and at the same time, the language has a very clean structure as compared to other languages.  So if you mastered your python concepts and skills, you can dominate these 7 domains. Machine learning / Artificial intelligence Desktop GUI Data analytics and data visualization  Web development Game development Mobile ap...

Different domains of Artificial intelligence(AI)

Artificial intelligence is a computer system that is able to perform tasks that ordinarily require human intelligence. Artificial intelligence systems are critical for companies that wish to extract value from data by automating and optimizing processes or producing actionable insights. There are certain domains of artificial intelligence on which we can create our expertise Machine learning Deep learning Robotics Expert systems Fuzzy logic Natural language processing Computer vision  1. Machine learning Machine learning is a subset of artificial intelligence. Machine learning enables computers or machines to make data-driven decisions rather than being explicitly programmed for a certain task. These programs or algorithms are designed in a way that they learn and improve over time when are exposed to new data. Different types of machine learning models Supervised learning Unsupervised learning Reinforcement learning Use cases Product recommendation on a shopping website. spam fil...

Top 5 free machine learning courses with certificate

In the era of 21st-century artificial intelligence, machine learning and data science became the most demanding and highest paying skills. After the covid-19 pandemic situation, the working style of the corporate sectors and the business had completely changed, now most of the business deals are made on the basis of data analysis, or when it comes to making the businesses automation the people hire a  machine learning engineer.    Hence it becomes really important for those who work in the corporate sector or a student pursuing a degree to get a job should update himself with these skills.  So, I listed you Top 5 machine learning courses from one of the leading organisations with completion certificates at free of cost. 1. Machine learning in the cloud with AWS batch About This course describes how to run and accelerate your machine learning applications in the cloud using AWS batch. AWS Batch is a fully managed service that allows you to easily and efficiently run b...