<jupyter_start><jupyter_text>Decision Tree - Train and Visualize  Prediction using Decision Tree Algorithm● Create the Decision Tree classifier and visualize it graphically.● The purpose is if we feed any new data to this classifier, it would be able to predict the right class accordingly.  Import Libraries:<jupyter_code>import pandas as pd

from sklearn.externals.six import StringIO
from sklearn.tree import DecisionTreeClassifier, export_graphviz

import pydotplus
import graphviz

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Iris Data Set:Load the csv file and define the input and output variables<jupyter_code>#define the column names for the pandas dataframe
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'iris_class']

#load the csv file into a new pandas dataframe
iris_df = pd.read_csv('iris_data.csv', names = column_names, header = None)

#Load the input variables into X
X = iris_df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].values

#Load the output variable into y
y = iris_df['iris_class'].values<jupyter_output><empty_output><jupyter_text>Modelling:Model the data using Desision Tree Classification model<jupyter_code>#Create an instance of the decision tree classifier
dec_tree = DecisionTreeClassifier(criterion="entropy")

#Fit the model with the training data X_train and y_train
dec_tree.fit(X, y)<jupyter_output><empty_output><jupyter_text>Visualization:Visualize the Decision Tree.<jupyter_code>#initialize a StringIO class
dot_data = StringIO()

#file name to save the image
filename = "iris_flower_tree.png"

feature_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'] #input values
class_names = ['Iris Setosa', 'Iris Versicolour', 'Iris Virginica'] #output values

#convert the decision tree model into dot data
out = export_graphviz(dec_tree,
                      feature_names=feature_names,
                      out_file=dot_data,
                      class_names=class_names,
                      filled=True,
                      special_characters=True,
                      rotate=False)

#convert the dot data into a graph
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

#save the graph
graph.write_png(filename)

#open and plot the graph
img = mpimg.imread(filename)
plt.figure(figsize=(100, 200))
plt.imshow(img, interpolation='nearest');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08182184602982669, 'fraction_numerical': 0.0024183796856106408, 'mean_word_length': 5.038929440389294, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '465099', 'license': 'MIT', 'n_tokens_mistral': 786, 'n_tokens_neox': 749, 'n_words': 228, 'path': 'Task  6 Decision Tree Algorithm.ipynb', 'repo_name': 'Nidhi-cdac/The-Sparks-Foundation-Tasks', 'size': '2481'}