<jupyter_start><jupyter_text>TASK 4 : Prediction Using Decision Tree Algorithm* Task is to create the Decision Tree classifier and visualize it graphically for *iris* dataset.* The main purpose is if we feed any new data to this classifier,it should be able to predict the right class accordingly.   1. Importing libraries<jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot
import seaborn as sns<jupyter_output><empty_output><jupyter_text>2. Exploring and Analysing the data<jupyter_code># loading dataset
data = pd.read_csv('Iris.csv')
data.head(10)

# species of iris dataset
data['Species'].unique()
# encoding species names
species = {
    'Iris-setosa' : 0,
    'Iris-versicolor' : 1,
    'Iris-virginica' : 2
}

data['Species'] = data['Species'].map(species)
data.Species.unique()
# splitting input and output data
X = data.iloc[:,1:5].values
y = data.iloc[:,5].values
# To check if data is split accordingly
print(X.shape)
print(y.shape)
# check for balanced dataset
sns.countplot(y)
pyplot.show()<jupyter_output>/home/kuluruvineeth/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>Observation* Data is equally balanced among three species.  3. Train Test Split<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=7)<jupyter_output><empty_output><jupyter_text>4. Decision Tree Classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
print(y_pred)<jupyter_output>[2 1 0 1 2 0 1 1 0 1 2 1 0 2 0 2 2 2 0 0 1 2 1 1 1 2 1 1 2 2]<jupyter_text>5. Evaluating Algorithm<jupyter_code>from sklearn import metrics
pyplot.figure(figsize=(10,8))
sns.heatmap(metrics.confusion_matrix(y_test,y_pred),xticklabels=data.iloc[:,1:5].columns.values,yticklabels=data.iloc[:,1:5].columns.values,center=0)
pyplot.show()
print(f"Precision: {metrics.precision_score(y_test, y_pred, average = 'macro')}")
print(f"Recall: {metrics.recall_score(y_test, y_pred, average = 'macro')}")
print(f"F1 Score: {metrics.f1_score(y_test, y_pred, average = 'macro')}")<jupyter_output>Precision: 0.883838383838384
Recall: 0.883838383838384
F1 Score: 0.883838383838384<jupyter_text>6. Visualizing Decision Tree<jupyter_code>from sklearn import tree

pyplot.figure(figsize=(15,10))
tree.plot_tree(model,feature_names=data.iloc[:,1:5].columns.values,filled=True)
pyplot.title('Decision Tree for Iris Dataset')
pyplot.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11170024743725698, 'fraction_numerical': 0.04277129727819017, 'mean_word_length': 6.774725274725275, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '536404', 'license': 'MIT', 'n_tokens_mistral': 1081, 'n_tokens_neox': 991, 'n_words': 252, 'path': 'TASK-4/Decision Tree Classifier.ipynb', 'repo_name': 'kuluruvineeth/TSF-Internship', 'size': '2829'}