-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_decisionTree.py
More file actions
174 lines (139 loc) · 4.66 KB
/
03_decisionTree.py
File metadata and controls
174 lines (139 loc) · 4.66 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 20:06:27 2021
@author: kalaivanan
Decision tree algorithm
entropy - scikit
Gini - scikit
plot the ouput
efficiency - confusion matrix
"""
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import numpy as np
# Importing the required packages
# Function importing Dataset
def impData(file):
df = pd.read_csv('E:\\Tech\\ML\\Data_Set\\'+file, sep= ',', header = 1)
print ("Dataset Length: ", len(df))
print ("Dataset Shape: ", df.shape)
print ("Dataset: ",df.head())
return df
# Function to split the dataset
def splitdataset(df):
# Separating the target variable
X = []
Y = []
X = df.values[:, 0:-2]
Y = df.values[:, -1]
# Splitting the dataset into train and test
X_train = []
X_test = []
y_train = []
y_test = []
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3, random_state = np.random)
#print(len(X_train))
#print(len(X_test))
#print(len(y_train))
#print(len(y_test.count())
return (X, Y, X_train, X_test, y_train, y_test)
# Function to perform training with giniIndex.
def train_using_gini(X_train, X_test, y_train):
# Creating the classifier object
clf_gini = DecisionTreeClassifier(criterion = "gini",
random_state = np.random,max_depth=3, min_samples_leaf=5)
# Performing training
clf_gini.fit(X_train, y_train)
return clf_gini
# Function to perform training with entropy.
def train_using_entropy(X_train, X_test, y_train):
# Decision tree with entropy
clf_entropy = DecisionTreeClassifier(
criterion = "entropy", random_state = np.random,
max_depth = 3, min_samples_leaf = 5)
# Performing training
clf_entropy.fit(X_train, y_train)
return clf_entropy
# Function to make predictions
def prediction(X_test, clf_object):
# Predicton on test with giniIndex
y_pred = []
y_pred = clf_object.predict(X_test)
#print("Predicted values:")
#print(y_pred)
return y_pred
# Function to calculate accuracy
def cal_accuracy(y_test, y_pred):
#print("Confusion Matrix: ", confusion_matrix(y_test, y_pred))
#print ("Accuracy : ", accuracy_score(y_test,y_pred)*100)
#print("Report : ", classification_report(y_test, y_pred))
err = 100 - accuracy_score(y_test,y_pred)*100
return err
# Driver code
def main(nTrain, file):
global err_entropy
global err_gini
err_entropy = []
err_gini = []
# Building Phase
data = impData(file)
for i in range(nTrain):
X = []
Y = []
X_train = []
X_test = []
y_train = []
y_test = []
X, Y, X_train, X_test, y_train, y_test = splitdataset(data)
clf_gini = train_using_gini(X_train, X_test, y_train)
X = []
Y = []
X_train = []
X_test = []
y_train = []
y_test = []
X, Y, X_train, X_test, y_train, y_test = splitdataset(data)
clf_entropy = train_using_entropy(X_train, X_test, y_train)
# Operational Phase
#print("Results Using Gini Index:")
# Prediction using gini
y_pred_gini = []
y_pred_gini = prediction(X_test, clf_gini)
err_gini.append(cal_accuracy(y_test, y_pred_gini))
#print("Results Using Entropy:")
# Prediction using entropy
y_pred_entropy = []
y_pred_entropy = prediction(X_test, clf_entropy)
err_entropy.append(cal_accuracy(y_test, y_pred_gini))
#print(err_entropy)
#print(err_gini)
plot_grad_descent(nTrain)
def plot_grad_descent(nTrain):
plt.figure(figsize =(10, 6))
plt.plot(range(nTrain),
err_gini,
color = 'blue',
linestyle='dashed',
marker='o',
markerfacecolor='red',
markersize = 10)
plt.plot(range(nTrain),
err_entropy,
color='grey',
linestyle= 'dashed',
marker='o',
markerfacecolor= 'red',
markersize = 10)
plt.xlabel('No. of splits')
plt.ylabel('Error Rate')
plt.title('Error Plot')
'''
# Calling main function
if __name__=="__main__":
main()
'''