Experiment 3: Name: Reena Kale Te Comps Roll No:23

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment 3

Name: Reena Kale TE COMPS Roll No:23

AIM: Decision Tree implementation using Programming language

THEORY:

Decision Tree : Decision tree is the most powerful and popular tool for classification and
prediction.
A Decision tree is a flowchart like tree structure, where each internal node denotes a test on
an
attribute, each branch represents an outcome of the test, and each leaf node (terminal node)
holds a
class label.
Construction of Decision Tree : A tree can be “learned” by splitting the source set into
subsets based
on an attribute value test. This process is repeated on each derived subset in a recursive
manner called
recursive partitioning. The recursion is completed when the subset at a node all has the
same value of
the target variable, or when splitting no longer adds value to the predictions. The construction
of
decision tree classifier does not require any domain knowledge or parameter setting, and
therefore is
appropriate for exploratory knowledge discovery. Decision trees can handle high dimensional
data. In
general decision tree classifier has good accuracy. Decision tree induction is a typical
inductive
approach to learn knowledge on classification.

ALGORITHM:
 Create node N
 If tuples in D are all of same class C, then
Return N as leaf node labeled with class C
 If attribute_list empty then
Return N as leaf node labeled with majority class in D or majority voting
 Apply attribute_selection_method (D, attribute_list)
 To find best splitting criterion
 Label node N with splitting criterion
 If splitting_attribute is discrete valued and multiway splits allowed then
attribute_list=splitting attribute
for each outcome j of splitting criterion
let Dj be set of data tuples in D satisfying outcome j
if Dj is empty then
attach a leaf labeled with the majority
class in D to node N
else
attach node returned by Generate decision tree(Dj, attribute_list) to node N
end for
 Return N

QUESTION:
Buying_Price Maintenance_Price Lug_Boot Safety Evaluation?
High High Small High Unacceptable
High High Small Low Unacceptable
Medium High Small High Acceptable
Low Medium Small High Acceptable
Low Low Big High Acceptable
Low Low Big Low Unacceptable
Medium Low Big Low Acceptable
High Medium Small High Unacceptable
High Low Big High Acceptable
Low Medium Big High Acceptable
High Medium Big Low Acceptable
Medium Medium Small Low Acceptable
Medium High Big High Acceptable
Low Medium Small Low Unacceptable

CODE:

from sklearn import preprocessing import


pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Cl assifier
from sklearn.model_selection import train_test_split calculation from
sklearn import metrics
from sklearn.tree import export_graphviz
from six import StringIO
from IPython.display import Image
import pydotplus import graphviz
df = pd.read_excel('sample.xlsx')
col_names = ["Buying_Price", "Maintenance_Price", "Lug_Boot", "Safety"] print(df.head())
le = preprocessing.LabelEncoder() for
column_name in df.columns:
if df[column_name].dtype == object: df[column_name] =
le.fit_transform(
df[column_name])
else:
pass
# print(df)
X = df[col_names] y =
df.Evaluation
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3,
random_state=1)

clf = DecisionTreeClassifier() clf =


clf.fit(X_train, y_train) dot_data =
StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True, feature_names=col_names, class_na
mes=['Acceptable','Unacceptable'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) graph.write_png('tree.png')
Image(graph.create_png())

OUTPUT:

CONCLUSION:
We successfully understood and implemented the concept of Decision Tree using
programming language.

You might also like