D Dev Duniya
Back to Journal
3 min read

Random Forest Classification Algorithm in Machine Learning | DevDuniya

Ashish yadav

Ashish yadav

Published on Dev Duniya
Keep exploring
Compare the details before you buy.

Random Forest is a powerful ensemble learning method that combines multiple decision trees to create a more robust and accurate prediction model. It's a versatile algorithm used for both classification and regression tasks.

How Random Forest Works

1. Create Multiple Decision Trees:

  • Random Forest builds an ensemble of multiple decision trees.
  • Each decision tree is trained on a different subset of the training data.
  • These subsets are created using bootstrapping, where samples are drawn with replacement from the original dataset. This means that some data points may appear multiple times in a single subset, while others may not appear at all.

2. Feature Randomization:

  • In addition to bootstrapping, Random Forest introduces feature randomness.
  • At each node of each decision tree, only a random subset of features is considered for splitting.
  • This further increases diversity among the trees in the ensemble.

3. Make Predictions:

  • To make a prediction for a new data point, each decision tree in the forest makes a prediction.
  • For classification: The most frequent class among the predictions of all trees is selected as the final prediction.
  • For regression: The average of the predictions from all trees is taken as the final prediction.

Key Advantages of Random Forest

  • High Accuracy: Often achieves high accuracy due to the ensemble nature and the introduction of randomness.
  • Handles High-Dimensional Data: Can effectively handle datasets with many features.
  • Robust to Overfitting: Reduces overfitting by averaging predictions from multiple trees and using feature randomness.
  • Handles Missing Values: Can handle missing values gracefully.
  • Feature Importance: Provides a measure of the importance of each feature in the model.

Random Forest Algorithm Example:

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a Random Forest classifier
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42) 

# Train the model
rf_classifier.fit(X_train, y_train)

# Make predictions
y_pred = rf_classifier.predict(X_test)

# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Import necessary libraries:

  • RandomForestClassifier: Imports the Random Forest classifier from scikit-learn.
  • load_iris: Loads the iris dataset.
  • train_test_split: Splits the data into training and testing sets.
  • accuracy_score: Calculates the accuracy of the model.

Load and prepare the data:

  • Load the iris dataset.
  • Split the data into training and testing sets.

Create and train the Random Forest model:

  • Create a RandomForestClassifier object with the desired number of trees (n_estimators).
  • Train the model using the fit() method with the training data.

Make predictions and evaluate:

  • Use the trained model to predict the class labels for the test data.
  • Calculate and print the accuracy of the model.

This example demonstrates a basic implementation of the Random Forest algorithm. You can experiment with different hyperparameters (e.g., number of trees, maximum depth of trees) to further optimize the model's performance.

Keep reading

12 latest articles

The newest stories from Dev Duniya, excluding the article you are reading.

All articles →

From the product catalog

Explore device specifications

Specification cards update automatically from published catalog products, grouped by brand.

Apple specifications

Explore Apple devices →
Apple Apple iPhone Duo
Apple Apple iPhone Duo specifications

Apple iPhone Duo is a new-generation iPhone model focused on a sleek design, powerful performance, and everyday usability. Explore iPhone Duo specifications, features, display, cameras, battery, and expected price in the USA. Get the latest information about Apple iPhone Duo and its key features. Compare iPhone Duo with other popular Apple iPhone models.

Size (inches)
Open: 4.64 x 6.48 x 0.21 Closed: 4.64 x 3.31 x 0.44
Weight (ounces)
8.96
Display size (inches)
Inner: 7.6-inch (diagonal) all-screen OLED folding display Outer: 5.4-inch (diagonal) all-screen OLED display
Resolution (pixels)
Inner: 1878-by-2670-pixel resolution at 430 ppi Outer: 1398-by-2034-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro Max
Apple Apple iPhone 18 Pro Max specifications

Apple iPhone 18 Pro Max is a high-end flagship smartphone designed for powerful performance and advanced photography. Explore iPhone 18 Pro Max specifications, features, display, cameras, battery, and expected price in the USA. Discover the latest upgrades and key features of Apple’s largest Pro iPhone. Compare iPhone 18 Pro Max with other premium flagship smartphones.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.78
Display size (inches)
6.9-inch (diagonal) all-screen OLED display
Resolution (pixels)
2868-by-1320-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro
Apple Apple iPhone 18 Pro specifications

Apple iPhone 18 Pro is a premium flagship smartphone with advanced performance and an upgraded camera system. Explore iPhone 18 Pro specifications, features, display, cameras, battery, and expected price in the USA. Get the latest details about Apple’s new Pro model and its key upgrades. Compare iPhone 18 Pro features with other flagship smartphones.

Size (inches)
5.91 x 2.83 x 0.34
Weight (ounces)
7.44
Display size (inches)
6.3-inch (diagonal) all-screen OLED display
Resolution (pixels)
2622-by-1206-pixel resolution at 460 ppi
See all specifications →

Google specifications

Explore Google devices →

Samsung specifications

Explore Samsung devices →