In this article, we will see how to build a Random Forest Classifier using the Scikit-Learn library of Python programming language and in order to do this, we use the IRIS dataset which is quite a common and famous dataset. The Random forest or Random Decision Forest is a supervised Machine learning algorithm used for classification, regression, and other tasks using decision trees.
The Random forest classifier creates a set of decision trees from a randomly selected subset of the training set. It is basically a set of decision trees (DT) from a randomly selected subset of the training set and then It collects the votes from different decision trees to decide the final prediction.
In this classification algorithm, we will use IRIS flower datasets to train and test the model. We will build a model to classify the type of flower.
Code: checking our dataset content and features names present in it.
[‘setosa’ ‘versicolor’ ‘virginica’]
[‘sepal length (cm)’, ’sepal width (cm)’, ’petal length (cm)’, ’petal width (cm)’]
Code: Importing required libraries and random forest classifier module.
Code: Looking at a dataset
sepallength sepalwidth petallength petalwidth species 0 5.1 3.5 1.4 0.2 0 1 4.9 3.0 1.4 0.2 0 2 4.7 3.2 1.3 0.2 0 3 4.6 3.1 1.5 0.2 0 4 5.0 3.6 1.4 0.2 0
ACCURACY OF THE MODEL: 0.9238095238095239
Code: predicting the type of flower from the data set
This implies it is setosa flower type as we got the three species or classes in our data set: Setosa, Versicolor, and Virginia. Now we will also find out the important features or selecting features in the IRIS dataset by using the following lines of code.
Code: Calculating feature importance
petal width (cm) 0.458607 petal length (cm) 0.413859 sepal length (cm) 0.103600 sepal width (cm) 0.023933 dtype: float64
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course