Random - Forest - Classification - Ipynb - Colab
Random - Forest - Classification - Ipynb - Colab
ipynb - Colab
Random Forest
Random Forest is a popular ensemble learning method used for classification and regression tasks.
It builds multiple decision trees during training and merges their outputs to improve the overall performance and control overfitting.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import seaborn as sns
Now, Create an instance of the RandomForestClassifier and fit it to the training data.
▾ RandomForestClassifier i ?
RandomForestClassifier(random_state=42)
5. Make Predictions
After training the model, use it to make predictions on the test set.
Evaluate the model's performance using accuracy, confusion matrix, and classification report.
https://colab.research.google.com/drive/16K4hJAk69rMA02GQt94P7OiAJ4w8CsbS#scrollTo=wK43DJdLnrwo&printMode=true 1/3
2/20/25, 10:10 AM Random_Forest_Classification.ipynb - Colab
print(f'Accuracy: {accuracy:.2f}')
print('Confusion Matrix:')
print(conf_matrix)
print('Classification Report:')
print(class_report)
Accuracy: 1.00
Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
Classification Report:
precision recall f1-score support
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
Visualize the confusion matrix using a heatmap for better understanding the model
https://colab.research.google.com/drive/16K4hJAk69rMA02GQt94P7OiAJ4w8CsbS#scrollTo=wK43DJdLnrwo&printMode=true 2/3
2/20/25, 10:10 AM Random_Forest_Classification.ipynb - Colab
https://colab.research.google.com/drive/16K4hJAk69rMA02GQt94P7OiAJ4w8CsbS#scrollTo=wK43DJdLnrwo&printMode=true 3/3