<jupyter_start><jupyter_text>Task 06: Prediction using Decision Tree Algorithm Author: <NAME> Problem Statement: Create the Decision Tree classifier and visualize it graphically. The dataset is taken from: https://bit.ly/3kXTdox Importing required libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import tree
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Loading the IRIS dataset<jupyter_code>iris_df = pd.read_csv('Iris.csv')
iris_df.head(5)<jupyter_output><empty_output><jupyter_text>Dropping redundant values<jupyter_code>#Dropping Unneccessary Column
iris_df = iris_df.drop(["Id"], axis = "columns") 
iris_df.head()<jupyter_output><empty_output><jupyter_text>Columns of the dataframe<jupyter_code>iris_df.columns<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>x = iris_df.iloc[:,0:4].values
y = iris_df['Species'].values

print(x.shape)
print(y.shape)<jupyter_output>(150, 4)
(150,)<jupyter_text>Splitting Training and Testing Data<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2, random_state = 82)
# Defining the decision tree algorithm
from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier()
dtree.fit(x_train,y_train)
y_pred = dtree.predict(x_test)
print(y_pred)
print('Decision Tree Classifer Created')<jupyter_output>['Iris-virginica' 'Iris-virginica' 'Iris-setosa' 'Iris-setosa'
 'Iris-setosa' 'Iris-virginica' 'Iris-versicolor' 'Iris-versicolor'
 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-virginica'
 'Iris-versicolor' 'Iris-setosa' 'Iris-versicolor' 'Iris-setosa'
 'Iris-virginica' 'Iris-setosa' 'Iris-virginica' 'Iris-virginica'
 'Iris-versicolor' 'Iris-virginica' 'Iris-setosa' 'Iris-virginica'
 'Iris-versicolor']
Decision Tree Classifer Created<jupyter_text>Evaluating the model<jupyter_code>from sklearn.metrics import accuracy_score, confusion_matrix
cm =confusion_matrix(y_test, y_pred)
print(cm)

accuracy = accuracy_score(y_test,y_pred)*100
print('Accuracy of our model is ' + str(round(accuracy,2))+'%.')<jupyter_output>[[11  0  0]
 [ 0  8  1]
 [ 0  2  8]]
Accuracy of our model is 90.0%.<jupyter_text>Cross Validating<jupyter_code>from sklearn.model_selection import cross_val_score
cv_accuracy = cross_val_score(dtree,x_train,y_train,cv =5)
print(cv_accuracy)
print("Accuracy of Model with Cross Validation is "+ str(round(cv_accuracy.mean() * 100)) +'%.')<jupyter_output>[0.875      1.         1.         0.91666667 0.91666667]
Accuracy of Model with Cross Validation is 94.0%.<jupyter_text>Decision Tree Visualization  Textual Visualization<jupyter_code>text_representation = tree.export_text(dtree)
print(text_representation)<jupyter_output>|--- feature_3 <= 0.80
|   |--- class: Iris-setosa
|--- feature_3 >  0.80
|   |--- feature_3 <= 1.65
|   |   |--- feature_2 <= 5.00
|   |   |   |--- class: Iris-versicolor
|   |   |--- feature_2 >  5.00
|   |   |   |--- feature_1 <= 2.75
|   |   |   |   |--- class: Iris-versicolor
|   |   |   |--- feature_1 >  2.75
|   |   |   |   |--- class: Iris-virginica
|   |--- feature_3 >  1.65
|   |   |--- feature_2 <= 4.85
|   |   |   |--- feature_1 <= 3.10
|   |   |   |   |--- class: Iris-virginica
|   |   |   |--- feature_1 >  3.10
|   |   |   |   |--- class: Iris-versicolor
|   |   |--- feature_2 >  4.85
|   |   |   |--- class: Iris-virginica<jupyter_text>Graphical Visualization<jupyter_code>from sklearn.tree import plot_tree
plt.figure(figsize =(50,40))
plt.style.use("grayscale")
plot_tree(dtree, rounded= True, filled =True, feature_names = iris_df.columns)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1410691003911343, 'fraction_numerical': 0.030508474576271188, 'mean_word_length': 4.956521739130435, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '350141', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1481, 'n_words': 263, 'path': 'Task 06 - Prediction using Decision Tree Algorithm.ipynb', 'repo_name': 'RupaliShekhawat/TheSparksFoundationInternship', 'size': '3845'}