0% found this document useful (0 votes)
124 views3 pages

Random - Forest - Classification - Ipynb - Colab

The document outlines a step-by-step guide for implementing a Random Forest classifier using the Iris dataset in Python. It includes importing necessary libraries, loading the dataset, splitting it into training and testing sets, training the model, making predictions, evaluating performance, and visualizing the confusion matrix. The model achieved an accuracy of 1.00 with detailed evaluation metrics provided.

Uploaded by

mgiri63021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views3 pages

Random - Forest - Classification - Ipynb - Colab

The document outlines a step-by-step guide for implementing a Random Forest classifier using the Iris dataset in Python. It includes importing necessary libraries, loading the dataset, splitting it into training and testing sets, training the model, making predictions, evaluating performance, and visualizing the confusion matrix. The model achieved an accuracy of 1.00 with detailed evaluation metrics provided.

Uploaded by

mgiri63021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/20/25, 10:10 AM Random_Forest_Classification.

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.

keyboard_arrow_down For Classification


1. Import Libraries

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

2. Load the Dataset

We will load the Iris dataset from sklearn.

# Load the iris dataset


iris = load_iris()
X = iris.data # Features
y = iris.target # Target variable

3. Split the Dataset

Split the dataset into training and testing sets.

# Split the dataset 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)

4. Create and Train the Random Forest Model

Now, Create an instance of the RandomForestClassifier and fit it to the training data.

# Create a Random Forest Classifier


rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

# Train the model


rf_classifier.fit(X_train, y_train)

▾ RandomForestClassifier i ?

RandomForestClassifier(random_state=42)

5. Make Predictions

After training the model, use it to make predictions on the test set.

# Make predictions on the test set


y_pred = rf_classifier.predict(X_test)

6. Evaluate the Model

Evaluate the model's performance using accuracy, confusion matrix, and classification report.

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)

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

0 1.00 1.00 1.00 10


1 1.00 1.00 1.00 9
2 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

7. Visualize the Confusion Matrix

Visualize the confusion matrix using a heatmap for better understanding the model

# Visualize the confusion matrix


plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=iris.target_names, yticklabels=iris.target_names)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title('Confusion Matrix')
plt.show()

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy