Skip to main content

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

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...


Popular posts from this blog

What is machine learning and it's types?

 What is machine learning(ML)? 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. Examples:- 1. Product recommendations   While checking for a product did you noticed when it recommends a product similar to what you are looking for? or did you noticed "the person bought this product also bought this" combination of products? How are they doing this recommendation? This is machine learning. 2. Email spam and malware filtering  There are a number of spam filtering approaches that email clients use.  To ascertain that these spam filters are continuously updated they are powered by machine learning. 3. Online customer support A number of websites nowadays offer the option to chat with customer support representati...

PIC18 Timer programming in C

The PIC18 timer is divided into 4 types Timer 0 Timer 1 Timer 2 Timer 3 PIC18 timers can be used to generate a time delay or as a counter to count external event happening outside the microcontroller. In this article, we will see how to generate a time delay by programming the PIC18 timer. Timer 0 The timer 0 module has the following features Software is scalable as an 8 bit or 16-bit timer/counter. Readable and writable Dedicated 8 bit software programmable Prescaler Clock source selectable to be internal or external Edge select for external clock Register required for Timer 0 Control register Each timer has a control register called TCON to set the various timer operation modes. T0CON is an 8-bit register used for control of timer 0. TOCON TMR0ON (Timer0 on/off control bit)                                       1 = Enable timer 0                ...

Interface relay with PIC18 microcontroller

Hi... today we will talk about another important component used in the embedded domain called a relay. Relays are electric switch which uses electromagnetism to either form or breaks the existing circuits. With the help of a relay, you can trigger a high voltage operation by a low voltage input signal. Relay is a highly versatile component that is as effective in a complex circuit as in a simple circuit. In this article, we will talk about how to interface relay with PIC18 controller. We will cover the topics in the following points: About the interfacing task Software tools used Required components Circuit diagram Code for controller Upload the HEX file Run simulation About the interfacing task In the interface relay task, with the help of a push-button, we will trigger the relay to control the lighting of the bulb which is of higher voltage. When we press the push button the microcontroller will trigger the relay to change its state, when the relay changes its state from normally clo...