Machine Learning - Random Forest
Machine Learning - Random Forest
Step 1 − First, start with the selection of random samples from a given dataset.
Step 2 − Next, this algorithm will construct a decision tree for every sample. Then
it will get the prediction result from every decision tree.
Step 3 − In this step, voting will be performed for every predicted result.
Step 4 − At last, select the most voted prediction result as the final prediction
result.
The following diagram illustrates how the Random Forest Algorithm works −
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 1/6
7/1/24, 10:56 AM Machine Learning - Random Forest
Random Forest is a flexible algorithm that can be used for both classification and
regression tasks. In classification tasks, the algorithm uses the mode of the predictions of
the individual trees to make the final prediction. In regression tasks, the algorithm uses
the mean of the predictions of the individual trees.
Handles Missing Data − Random Forest algorithm can handle missing data
without the need for imputation. This is because the algorithm only considers the
features that are available for each data point and does not require all features to
be present for all data points.
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 2/6
7/1/24, 10:56 AM Machine Learning - Random Forest
We will begin by importing the necessary libraries. We will be using the pandas library for
data manipulation, and the scikit-learn library for implementing the Random Forest
algorithm.
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learningdatabases/iris/
Before we can use the data to train our model, we need to preprocess it. This involves
separating the features and the target variable and splitting the data into training and
testing sets.
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 3/6
7/1/24, 10:56 AM Machine Learning - Random Forest
Next, we will train our Random Forest classifier on the training data.
Once we have trained our model, we can use it to make predictions on the test data.
Finally, we will evaluate the performance of our model using various metrics such as
accuracy, precision, recall, and F1-score.
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 4/6
7/1/24, 10:56 AM Machine Learning - Random Forest
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 5/6
7/1/24, 10:56 AM Machine Learning - Random Forest
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
Output
This will give us the performance metrics of our Random Forest classifier as follows −
Accuracy: 0.9811320754716981
Precision: 0.9821802935010483
Recall: 0.9811320754716981
F1-score: 0.9811157396063056
https://www.tutorialspoint.com/machine_learning/machine_learning_random_forest_classification.htm 6/6