0% found this document useful (0 votes)
3 views2 pages

Prac 4

The document outlines a program that implements a decision tree algorithm using the Iris dataset. It includes steps for data preparation, model training, tree plotting, and confusion matrix visualization. The decision tree is visualized with a light yellow background, and a heatmap of the confusion matrix is generated to evaluate model predictions.

Uploaded by

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

Prac 4

The document outlines a program that implements a decision tree algorithm using the Iris dataset. It includes steps for data preparation, model training, tree plotting, and confusion matrix visualization. The decision tree is visualized with a light yellow background, and a heatmap of the confusion matrix is generated to evaluate model predictions.

Uploaded by

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

Program to implement decision tree algorithm and plot the tree

# Importing the required libraries


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier

# Loading the dataset


iris = load_iris()

# Converting the data to a pandas dataframe


data = pd.DataFrame(data=iris.data, columns=iris.feature_names)

# Creating a separate column for the target variable of the iris dataset
data['Species'] = iris.target

# Replacing the categories of the target variable with the actual names of the
species
target = np.unique(iris.target)
target_n = np.unique(iris.target_names)
target_dict = dict(zip(target, target_n))
data['Species'] = data['Species'].replace(target_dict)

# Separating the independent and dependent variables of the dataset


x = data.drop(columns="Species")
y = data["Species"]
names_features = x.columns
target_labels = y.unique()
# Splitting the dataset into training and testing datasets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3,
random_state=93)

# Creating an instance of the classifier class


dtc = DecisionTreeClassifier(max_depth=3, random_state=93)

# Fitting the training dataset to the model


dtc.fit(x_train, y_train)

# Plotting the Decision Tree with a light background color


plt.figure(figsize=(30, 10), facecolor='lightyellow') # Changed from blue to light
yellow
Tree = tree.plot_tree(dtc, feature_names=names_features,
class_names=target_labels, rounded=True, filled=True, fontsize=14)
plt.show()

# Predicting the target variable


y_pred = dtc.predict(x_test)
# Finding the confusion matrix
confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
matrix = pd.DataFrame(confusion_matrix)
# Plotting the heatmap for the confusion matrix
sns.set(font_scale=1.3)
plt.figure(figsize=(10, 7))
sns.heatmap(matrix, annot=True, fmt="g", cmap="magma") # Colormap
remains magma
plt.title('Confusion Matrix')
plt.xlabel("Predicted Values")
plt.ylabel("True Labels")
plt.xticks(ticks=np.arange(len(target_labels)), labels=target_labels, rotation=45)
plt.yticks(ticks=np.arange(len(target_labels)), labels=target_labels, rotation=0)
plt.show()

You might also like