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

Decision Tree Exp 5 DWM

The document loads and analyzes the iris dataset using a decision tree classifier. It splits the data into training and test sets, fits a decision tree model to the training set, makes predictions on the test set, and calculates a confusion matrix to evaluate the model performance.

Uploaded by

jimmythewarriorr
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)
8 views2 pages

Decision Tree Exp 5 DWM

The document loads and analyzes the iris dataset using a decision tree classifier. It splits the data into training and test sets, fits a decision tree model to the training set, makes predictions on the test set, and calculates a confusion matrix to evaluate the model performance.

Uploaded by

jimmythewarriorr
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/ 2

08/09/2023, 10:19 Decision tree exp 5 DWM - Colaboratory

1 # Importing the required libraries


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt
5 from sklearn import metrics
6 import seaborn as sns
7 from sklearn.datasets import load_iris
8 from sklearn.model_selection import train_test_split
9 from sklearn import tree

1 # Loading the dataset


2 iris = load_iris()

1 #converting the data to a pandas dataframe


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

1 #creating a separate column for the target variable of iris dataset


2 data['Species'] = iris.target

1 #replacing the categories of target variable with the actual names of the species 23.target = np.unique(iris.target)
2 target = np.unique(iris.target)
3 target_n = np.unique(iris.target_names)
4 target_dict = dict(zip(target, target_n))
5 data['Species'] = data['Species'].replace(target_dict)

1 # Separating the independent dependent variables of the dataset


2 x = data.drop(columns = "Species")
3 y = data["Species"]
4 names_features = x.columns
5 target_labels = y.unique()

1 # Splitting the dataset into training and testing datasets


2 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 93)

1 # Importing the Decision Tree classifier class from sklearn


2 from sklearn.tree import DecisionTreeClassifier

1 # Creating an instance of the classifier class


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

1 # Fitting the training dataset to the model


2 dtc.fit(x_train, y_train)

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

1 # Plotting the Decision Tree


2 plt.figure(figsize = (30, 10), facecolor = 'b')
3 Tree = tree.plot_tree(dtc, feature_names = names_features, class_names = target_labels, rounded = True, filled = True, fontsize = 14)
4 plt.show()
5 y_pred = dtc.predict(x_test)
6 # Finding the confusion matrix
7 confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
8 matrix = pd.DataFrame(confusion_matrix)
9 axis = plt.axes()
10 sns.set(font_scale = 1.3)
11 plt.figure(figsize = (10,7))
12 # Plotting heatmap
13 sns.heatmap(matrix, annot = True, fmt = "g", ax = axis, cmap = "magma")
14 axis.set_title('Confusion Matrix')
15 axis.set_xlabel("Predicted Values", fontsize = 10)
16 axis.set_xticklabels([''] + target_labels)
17 axis.set_ylabel( "True Labels", fontsize = 10)
18 axis.set_yticklabels(list(target_labels), rotation = 0)
19 plt.show()

https://colab.research.google.com/drive/1Hf0408-6iqvmzvC8D56OUapKts0UudKa#scrollTo=CyQ3zvPSE2j-&printMode=true 1/2
08/09/2023, 10:19 Decision tree exp 5 DWM - Colaboratory

<Figure size 1000x700 with 0 Axes>

Colab paid products - Cancel contracts here

check 2s completed at 10:16

https://colab.research.google.com/drive/1Hf0408-6iqvmzvC8D56OUapKts0UudKa#scrollTo=CyQ3zvPSE2j-&printMode=true 2/2

You might also like