Ad Code

Predicting class of flower using Decision tree algorithm

Predicting class of flower using Decision tree algorithm


Decision tree on iris dataset



Hi... in this task we gonna determine the class or type of the flower based on its features. The dataset we gonna use is iris dataset. You can use built-in iris dataset or goto UCI repository to download. 

We gonna use the iris built-in dataset on sklearn for this task.

In this task, the machine learning model we gonna built is the decision tree algorithm.

The decision tree is a type of supervised machine learning.where the data is continuously split according to a certain parameter.

But, before that let's quickly recap what is a decision tree.

Decision tree

  • Decision tree comes under the category of supervised machine learning.
  • Decision tree classification models in the form of a tree structure.
  • It breaks down a dataset into smaller and smaller subsets.
  • Decision trees are commonly used in decision analysis to help identify a strategy most likely to reach a goal.




We will cover this task on the following steps:

1. Import required libraries
2. Load the iris dataset
3. Prepare the data for the ML model
4. Create the ML model and train it
5. Visualize the ML model 
6. Test the Model 
7. Check the accuracy


1. Import required libraries 


For this task, the libraries we gonna use are NumPy, pandas, Matplotlib and Sklearn.

code 

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from matplotlib import pyplot as plt
from sklearn.metrics import accuracy_score


2. Load the iris dataset

 
After importing all the libraries now we gonna load the iris dataset to the data frame and observe all its features.

code 

iris=load_iris()
dataframe=pd.DataFrame(iris.data,columns=iris.feature_names)
dataframe.head()

Data frame of iris data set


3. Prepare the data for the ML model 


Now it's time to prepare the data for our model. 

To prepare the train data and the test data we gonna use the train_test_split built-in function of the sklearn.

This function will split the data features and data-target into the train data and test data.

This section of the code we split the 120 train dataset and 30 test data.

code   

x=iris.data
y=iris.target
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
print("x train data", x_train.shape)
print("x test data", x_test.shape)
print("y train data", y_train.shape)
print("y test data", y_test.shape)


4. Create the ML model and train it 


Now it's time to create the Machine Learning Model. 

For this task, we gonna use Decision Tree Classifier. 

We will fit the train data and test data to this classifier.


code 

model=DecisionTreeClassifier()
model.fit(x_train,y_train)
print("model is trained")


5. Visualize the ML model 


In this section, we will visualize the decision made by the decision tree to split the data according to a certain parameter.

To do that we gonna use the plot_tree function.

code 

plt.figure(figsize=(20,10))
tree.plot_tree(model, filled=True)

visualizarion of decision tree


6. Test the Model 


Finally, it's time to check our model. 

To do that we just gonna fit the test data to the model and get the predicted value.

After getting the predicted value we will compare it to the actual value.

code 

prediction=model.predict(x_test)
prediction 

df=pd.DataFrame({'predicted value':prediction,'actual value':y_test})
df['label']=df['predicted value'].replace(dict(enumerate(iris.target_names)))
df.head()

this is data comparison between the actual and predicted output from decision tree classifier

7. Check the accuracy  


The last step let's check the accuracy of our model. 

To do that we gonna use accuracy_score function to to check the accuracy.


code 

score=accuracy_score(prediction,y_test)
print(f"Accuracy score is {score} i.e. {score*100}%")


Video demonstration 








Follow me on: 

Linkedin - https://www.linkedin.com/in/somen-das-6a933115a/  

Instagram - https://www.instagram.com/somen912/?hl=en

Github - https://github.com/somen912/Technical-tasks/blob/main/Task-3%20Decision%20tree.ipynb

And don't forget to subscribe to the blog.

so...

Thanks for your time and stay creative...