Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Random Forest Algorithm in Machine Learning
Next article icon

Random Forest Algorithm in Machine Learning

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Random Forest is a machine learning algorithm that uses many decision trees to make better predictions. Each tree looks at different random parts of the data and their results are combined by voting for classification or averaging for regression. This helps in improving accuracy and reducing errors.

Working of Random Forest Algorithm

  • Create Many Decision Trees: The algorithm makes many decision trees each using a random part of the data. So every tree is a bit different.
  • Pick Random Features: When building each tree it doesn’t look at all the features (columns) at once. It picks a few at random to decide how to split the data. This helps the trees stay different from each other.
  • Each Tree Makes a Prediction: Every tree gives its own answer or prediction based on what it learned from its part of the data.
  • Combine the Predictions:
    • For classification we choose a category as the final answer is the one that most trees agree on i.e majority voting.
    • For regression we predict a number as the final answer is the average of all the trees predictions.
  • Why It Works Well: Using random data and features for each tree helps avoid overfitting and makes the overall prediction more accurate and trustworthy.

Random forest is also a ensemble learning technique which you can learn more about from: Ensemble Learning

Key Features of Random Forest

  • Handles Missing Data: It can work even if some data is missing so you don’t always need to fill in the gaps yourself.
  • Shows Feature Importance: It tells you which features (columns) are most useful for making predictions which helps you understand your data better.
  • Works Well with Big and Complex Data: It can handle large datasets with many features without slowing down or losing accuracy.
  • Used for Different Tasks: You can use it for both classification like predicting types or labels and regression like predicting numbers or amounts.

Assumptions of Random Forest

  • Each tree makes its own decisions: Every tree in the forest makes its own predictions without relying on others.
  • Random parts of the data are used: Each tree is built using random samples and features to reduce mistakes.
  • Enough data is needed: Sufficient data ensures the trees are different and learn unique patterns and variety.
  • Different predictions improve accuracy: Combining the predictions from different trees leads to a more accurate final result.

Implementing Random Forest for Classification Tasks

Here we will predict survival rate of a person in titanic.

  • Import libraries and load the Titanic dataset.
  • Remove rows with missing target values ('Survived').
  • Select features like class, sex, age, etc and convert 'Sex' to numbers.
  • Fill missing age values with the median.
  • Split the data into training and testing sets, then train a Random Forest model.
  • Predict on test data, check accuracy and print a sample prediction result.
Python
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, classification_report import warnings warnings.filterwarnings('ignore')  url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv" titanic_data = pd.read_csv(url)  titanic_data = titanic_data.dropna(subset=['Survived'])  X = titanic_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare']] y = titanic_data['Survived']  X.loc[:, 'Sex'] = X['Sex'].map({'female': 0, 'male': 1})  X.loc[:, 'Age'].fillna(X['Age'].median(), inplace=True)  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)  rf_classifier.fit(X_train, y_train)  y_pred = rf_classifier.predict(X_test)  accuracy = accuracy_score(y_test, y_pred) classification_rep = classification_report(y_test, y_pred)  print(f"Accuracy: {accuracy:.2f}") print("\nClassification Report:\n", classification_rep)  sample = X_test.iloc[0:1] prediction = rf_classifier.predict(sample)  sample_dict = sample.iloc[0].to_dict() print(f"\nSample Passenger: {sample_dict}") print(f"Predicted Survival: {'Survived' if prediction[0] == 1 else 'Did Not Survive'}") 

Output:

clf
Random Forest for Classification Tasks

We evaluated model's performance using a classification report to see how well it predicts the outcomes and used a random sample to check model prediction.

Implementing Random Forest for Regression Tasks

We will do house price prediction here.

  • Load the California housing dataset and create a DataFrame with features and target.
  • Separate the features and the target variable.
  • Split the data into training and testing sets (80% train, 20% test).
  • Initialize and train a Random Forest Regressor using the training data.
  • Predict house values on test data and evaluate using MSE and R² score.
  • Print a sample prediction and compare it with the actual value.
Python
import pandas as pd from sklearn.datasets import fetch_california_housing from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_squared_error, r2_score  california_housing = fetch_california_housing() california_data = pd.DataFrame(california_housing.data, columns=california_housing.feature_names) california_data['MEDV'] = california_housing.target  X = california_data.drop('MEDV', axis=1) y = california_data['MEDV']  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  rf_regressor = RandomForestRegressor(n_estimators=100, random_state=42)  rf_regressor.fit(X_train, y_train)  y_pred = rf_regressor.predict(X_test)  mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred)  single_data = X_test.iloc[0].values.reshape(1, -1) predicted_value = rf_regressor.predict(single_data) print(f"Predicted Value: {predicted_value[0]:.2f}") print(f"Actual Value: {y_test.iloc[0]:.2f}")  print(f"Mean Squared Error: {mse:.2f}") print(f"R-squared Score: {r2:.2f}") 

Output:

reg
Random Forest for Regression Tasks

We evaluated the model's performance using Mean Squared Error and R-squared Score which show how accurate the predictions are and used a random sample to check model prediction.

Advantages of Random Forest

  • Random Forest provides very accurate predictions even with large datasets.
  • Random Forest can handle missing data well without compromising with accuracy.
  • It doesn’t require normalization or standardization on dataset.
  • When we combine multiple decision trees it reduces the risk of overfitting of the model.

Limitations of Random Forest

  • It can be computationally expensive especially with a large number of trees.
  • It’s harder to interpret the model compared to simpler models like decision trees.

Next Article
Random Forest Algorithm in Machine Learning

S

susmit_sekhar_bhakta
Improve
Article Tags :
  • Machine Learning
  • AI-ML-DS
  • AI-ML-DS With Python
Practice Tags :
  • Machine Learning

Similar Reads

    Machine Learning Algorithms
    Machine learning algorithms are essentially sets of instructions that allow computers to learn from data, make predictions, and improve their performance over time without being explicitly programmed. Machine learning algorithms are broadly categorized into three types: Supervised Learning: Algorith
    8 min read
    How to Choose Right Machine Learning Algorithm?
    Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. A machine-learning algorithm is a program with a particular manner of altering its own parameters
    4 min read
    Machine Learning Algorithms Cheat Sheet
    Machine Learning Algorithms are a set of rules that help systems learn and make decisions without giving explicit instructions. They analyze data to find patterns and hidden relationships. And using this information, they make predictions on new data and help solve problems. This cheatsheet will cov
    4 min read
    Top 6 Machine Learning Classification Algorithms
    Are you navigating the complex world of machine learning and looking for the most efficient algorithms for classification tasks? Look no further. Understanding the intricacies of Machine Learning Classification Algorithms is essential for professionals aiming to find effective solutions across diver
    13 min read
    Types of Machine Learning Algorithms
    Machine learning algorithms can be broadly categorized into three main types: supervised learning, unsupervised learning, and reinforcement learning. Each category serves different purposes and is used in various applications. Here's an overview of the types of machine learning algorithms:Machine Le
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences