<jupyter_start><jupyter_text>GRIP @ The Sparks Foundation Author : <NAME> Task 6 : Prediction using Decision Tree AlgorithmIn this task, for the given ‘Iris’ dataset I have created the Decision Tree classifier and visualized it graphically. The purpose of this task is if we feed any new data to this classifier, it would be able to predict the right class accordingly. Technical Stack  : Sikit Learn, Numpy Array, Seaborn, Pandas, Matplotlib, Pydot<jupyter_code># Importing the required libraries

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split
import sklearn.metrics as sm
from sklearn import tree

import pandas as pd
import numpy as np
import seaborn as sns

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>1 - Loading the dataset<jupyter_code># Reading the Iris dataset
df = pd.read_csv('csv/Iris.csv')

df.drop(['Id'],axis=1,inplace=True)
df.head()
# to find number of rows and columns in dataset

df.info()
df.shape
# checking if there are any null values in dataset
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>2 - Visualizing the data<jupyter_code># Label encoding

X=df.iloc[:,[0,1,2,3]].values

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
df.Species = le.fit_transform(df.Species)
y=df.iloc[:,[4]].values
y=y.reshape(-1)
# Input data Visualization
sns.pairplot(df)
 # Scatter plot of data based on Sepal Length and Width features
sns.FacetGrid(df,hue='Species').map(plt.scatter,'SepalLengthCm','SepalWidthCm').add_legend()
plt.show()

# Scatter plot of data based on Petal Length and Width features
sns.FacetGrid(df,hue='Species').map(plt.scatter,'PetalLengthCm','PetalWidthCm').add_legend()
plt.show()<jupyter_output><empty_output><jupyter_text>3 - Decision Tree Model Training<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1) 
dtc = DecisionTreeClassifier()
dtc.fit(X_train,y_train)
print("Model Training Completed")
y_pred = dtc.predict(X_test)<jupyter_output><empty_output><jupyter_text>4 - Comparing the Actual and Predicted Values<jupyter_code>dt = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred}) 
dt<jupyter_output><empty_output><jupyter_text>5 - Visualizing the trained model<jupyter_code>#Visualizing the trained Decision Tree Classifier taking all 4 features in consideration

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

fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (4,4), dpi = 300)

tree.plot_tree(dtc, feature_names = fn, class_names = cn, filled = True);<jupyter_output><empty_output><jupyter_text>6 - Predicting the class output for some random values of petal and sepal length and width<jupyter_code>print("Class Names = ",df['Species'].unique())

# Estimating class probabilities
print()
print("Estimating Class Probabilities for flower whose petals length width are 4.7cm and 3.2cm and sepal length and width are 1.3cm and 0.2cm. ")
print()
print('Output = ',dtc.predict([[4.7, 3.2, 1.3, 0.2]]))
print()
print("Our model predicts the class as 0, that is, setosa.")<jupyter_output>Class Names =  [0 1 2]

Estimating Class Probabilities for flower whose petals length width are 4.7cm and 3.2cm and sepal length and width are 1.3cm and 0.2cm. 

Output =  [0]

Our model predicts the class as 0, that is, setosa.<jupyter_text>7 - Calculating the Model's accuracy<jupyter_code># Model Accuracy
print("Accuracy:",sm.accuracy_score(y_test, y_pred))<jupyter_output>Accuracy: 1.0
{'fraction_non_alphanumeric': 0.11031439602868175, 'fraction_numerical': 0.015995587424158852, 'mean_word_length': 6.356997971602434, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '98781', 'license': 'MIT', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1230, 'n_words': 364, 'path': 'Decision_Tree.ipynb', 'repo_name': 'skimran2000/TSF-GRIP-Tasks', 'size': '3636'}