-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDecisionTree.py
75 lines (59 loc) · 2.62 KB
/
DecisionTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 20:48:04 2020
@author: Abhinav
"""
# Importing the libraries
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Purchase_History.csv')
#Method-1 (Handling Categorical Variables)
pd.get_dummies(dataset["Gender"])
pd.get_dummies(dataset["Gender"],drop_first=True)
S_Dummy = pd.get_dummies(dataset["Gender"],drop_first=True)
S_Dummy.head(5)
#Now, lets concatenate these dummy var columns in our dataset.
dataset = pd.concat([dataset,S_Dummy],axis=1)
dataset.head(5)
dataset.tail(2)
#dropping the columns whose dummy var have been created
dataset.drop(["Gender",],axis=1,inplace=True)
dataset.head(5)
#------------------------------------------------------------------------------
#Obtaining DV & IV from the dataset
X = dataset.iloc[:, [1,2,4]].values
y = dataset.iloc[:, 3].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 2)
# Fitting Decision Tree Classification to the Training set
from sklearn.tree import DecisionTreeClassifier
#classifier = DecisionTreeClassifier(criterion = 'entropy')
#If desired we can supply extra parameters to decision trees fxn, but
#it may or may not give better accuracy.
classifier = DecisionTreeClassifier(criterion = 'entropy',max_depth = 3, min_samples_leaf=5)
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
#Accuracy = 91%
# Decision Tree visualization-----------------
from sklearn import tree
#Simple Decision Tree
tree.plot_tree(classifier)
#image is quite blurred
#Lets try to make decision tree more interpretable by adding filling colors.
tree.plot_tree(classifier,filled = True)
#Although the Decision tree shows class name & leafs are colred but still its view is blurred.
#Lets create a blank chart of desired size using matplotlib library and place our Decision tree there.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=300)
#The above line is used to set the pixels of the Decision Trees nodes so that
#the content mentioned in each node of Decision tree is visible.
cn=['0','1']
tree.plot_tree(classifier,class_names=cn,filled = True)
#if you want save figure, use savefig method in returned figure object.
fig.savefig('imagename-IIM_Sirmaur.png')