-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDT_dataset.py
More file actions
60 lines (47 loc) · 1.69 KB
/
DT_dataset.py
File metadata and controls
60 lines (47 loc) · 1.69 KB
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
# Importing the libraries
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('/home/deepak/analytics/Purchase_History.csv')
X = dataset.iloc[:, [2,3]].values
y = dataset.iloc[:, 4].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.25, random_state = 0)
"""
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
"""
# Fitting Decision Tree Classification to the Training set
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
#max_depth = 3, min_samples_leaf=5)
clf=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%
"""
#Rescaling my independent variables:
X_test = sc.inverse_transform(X_test)
"""
'''
pip install dtreeplt #to install dtreeplt lib.
'''
# Decision Tree visualization
from dtreeplt import dtreeplt
dtree = dtreeplt( model=classifier, feature_names=X_test,target_names=y_test)
fig = dtree.view()
fig
#if you want save figure, use savefig method in returned figure object.
#When only Age IV is taken to build Decision tree
fig.savefig('DT_output_1.png')
#When only Salary IV is taken to build Decision tree
fig.savefig('DT_output_2.png')
#When both Income & Salary IV is taken to build Decision tree
fig.savefig('DT_output_3.png')