<jupyter_start><jupyter_text>Prediction using Decision Tree AlgorithmFor the given ‘Iris’ dataset, create the Decision Tree classifier and visualize itgraphically. The purpose is if we feed any new data to this classifier, it would be able topredict the right class accordingly.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.read_csv("Iris.csv")
print(df.head(5))<jupyter_output>Id  SepalLengthCm  SepalWidthCm  PetalLengthCm  PetalWidthCm      Species
0   1            5.1           3.5            1.4           0.2  Iris-setosa
1   2            4.9           3.0            1.4           0.2  Iris-setosa
2   3            4.7           3.2            1.3           0.2  Iris-setosa
3   4            4.6           3.1            1.5           0.2  Iris-setosa
4   5            5.0           3.6            1.4           0.2  Iris-setosa<jupyter_text>seaborn.pairplot(): To plot multiple pairwise bivariate distributions in a dataset. The pairplot() function shows the relationship for (n,2) combination of variable in a Dataframe as a matrix of plots and the diagonal plots are univariate plots.<jupyter_code>df.drop("Id",axis=1,inplace=True)
sns.pairplot(df,hue="Species")
df.describe()
df["Species"].value_counts()
# Defining the decision tree algorithm
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
#Splitting data into training and testing data
x=df.drop('Species',axis=1)
y=df.Species
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=100) 
#Creating classifier and training it on train data
dtree = DecisionTreeClassifier(max_depth=5)
dtree.fit(X_train,y_train)

print("Decision Tree Classifier Created")
#Predicting on test data
pred_X=dtree.predict(X_test)
pred_X
#Accuracy 
dtree.score(X_test,y_test)<jupyter_output><empty_output><jupyter_text>Let us visualize the Decision Tree to understand it better.<jupyter_code>import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree

X, y = load_iris(return_X_y=True)

# Make an instance of the Model
clf = DecisionTreeClassifier(max_depth = 5)

# Train the model on the data
clf.fit(X, y)

fn=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
cn=['setosa', 'versicolor', 'virginica']

# Setting dpi = 300 to make image clearer than default
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=300)

tree.plot_tree(clf,
           feature_names = fn, 
           class_names=cn,
           filled = True);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08692421991084695, 'fraction_numerical': 0.02637444279346211, 'mean_word_length': 3.400326797385621, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '342314', 'license': 'MIT', 'n_tokens_mistral': 930, 'n_tokens_neox': 889, 'n_words': 266, 'path': 'Task 3/TSF - Task3.ipynb', 'repo_name': 'Skywalker-19/TSF-Assignments', 'size': '2692'}