Decision Tree and Random Forest

 

Expt No: 6                                           Decision Tree and Random Forest

Date:

 

Aim: To write a program to demonstrate Decision Tree and Random Forest.

 

Program

 

# Decision Tree

import pandas as pd

from sklearn.tree import DecisionTreeClassifier, plot_tree

import matplotlib.pyplot as plt

 

# Load the Iris dataset from a CSV file and drop Index column

iris_df = pd.read_csv("Iris.csv")

iris_df.drop('Id', axis=1, inplace=True)

 

# Separate features (X) and target variable (y)

X = iris_df[['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']]

y = iris_df['Species']

 

# Initialize and train a decision tree classifier

clf = DecisionTreeClassifier()

clf.fit(X, y)

 

# Plot the decision tree

plt.figure(figsize=(12, 8))

plot_tree(clf, filled=True, feature_names=X.columns, class_names=clf.classes_)

plt.show()

 

 


 

# Random Forest

# Import necessary libraries

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import classification_report, confusion_matrix

from sklearn.tree import plot_tree

import matplotlib.pyplot as plt

 

# Load the Iris dataset from CSV file and drop Index field

iris_data = pd.read_csv("Iris.csv")

iris_data.drop('Id', axis=1, inplace=True)

 

# Split features and target variable

X = iris_data.drop('Species', axis=1)

y = iris_data['Species']

 

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,

random_state=42)

 

# Create a Random Forest classifier

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

 

# Train the classifier on the training data

rf_classifier.fit(X_train, y_train)

 

# Select one of the decision trees from the Random Forest

tree_to_display = rf_classifier.estimators_[0]

 


 

# Display the selected decision tree

plt.figure(figsize=(12, 12))

plot_tree(tree_to_display, feature_names=X.columns,

class_names=iris_data['Species'].unique(), filled=True)

plt.show()

 

 

 

 

Result: Thus the program to demonstrate Decision Tree and Random Forest was written and executed.

 

 Sample Output

 

# Decision Tree


# Random Forest 






 

 


 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu