Creating the decision tree classifier using Python
In this tutorial, we are creating a decision tree classifier for Iris dataset and visualizing it graphically in Python. The purpose is if we feed any data to this classifier, it would be able to predict the right class accordingly. Here, we are exploring the decision tree algorithm through this tutorial. This tutorial deals with understanding the working of decision trees.
Python program for creating the decision tree classifier
Decision Tree algorithm is a part of the family of supervised learning algorithms. Decision Tree is used to create a training model that can be used to predict the class or value of the target variable by learning simple decision rules inferred from training data. A decision tree is very useful in data exploration i.e, it is one of the best and the fastest way to identify the relation between two or more variables and to find the most significant variables. The below code will take us through the creation of a decision tree classifier.
Importing libraries in Python
import sklearn.datasets as datasets import pandas as pd from sklearn.tree import DecisionTreeClassifier from sklearn.externals.six import StringIO from IPython.display import Image import pydotplus from sklearn.tree import export_graphviz
Loading the iris dataset
data = datasets.load_iris() data
Forming the iris dataframe and displaying first 10 rows
df=pd.DataFrame(data.data, columns=data.feature_names) print(df.head(10))
Identifying the class labels
labels = data.feature_names labels
Now let us define the Decision Tree Algorithm
# Defining the decision tree algorithm dtree=DecisionTreeClassifier() dtree.fit(df,y) print('Decision Tree Classifer Created')
Let us visualize the Decision Tree to understand it better.
dot_data = StringIO() export_graphviz(dtree, out_file=dot_data, feature_names=labels, filled=True, rounded=True, special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) Image(graph.create_png())
By executing the above codes, we can create the decision tree classifier of iris dataset and visualize it.
You can now feed any new/test data to this classifier and it would be able to predict the right class accordingly.