Random Forest Algorithm in Machine Learning (original) (raw)

Last Updated : 30 May, 2025

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

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

**Key Features of Random Forest

Assumptions of Random Forest

Implementing Random Forest for Classification Tasks

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

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.

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

Limitations of Random Forest