-
Notifications
You must be signed in to change notification settings - Fork 2
/
DecisionTreeClass.py
132 lines (107 loc) · 5.45 KB
/
DecisionTreeClass.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
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
from sklearn import metrics
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from id3 import Id3Estimator
import six
import sys
from tkinter import *
sys.modules['sklearn.externals.six'] = six
class DecisionTreeClass:
def __init__(self, case):
self.treeData = pd.read_csv('Data.csv')
self.treeX = self.treeData.drop('Churn', axis=1)
self.treeY = self.treeData['Churn']
self.mainColor = "#191142"
self.root = Toplevel()
self.root.title("Service cancellation predictor")
self.root.configure(background=self.mainColor, padx=30, pady=30)
self.root.overrideredirect(True)
global DTx_train, DTx_test, DTy_train, DTy_test, estimator, DTclf
DTx_train, DTx_test, DTy_train, DTy_test = train_test_split(self.treeX, self.treeY, test_size=0.10, random_state=101)
estimator = Id3Estimator()
DTclf = estimator.fit(DTx_train, DTy_train)
global image
image = PhotoImage(file="Photos/Buttons/closeButton.png")
self.close_button = Button(self.root, image=image, background=self.mainColor, bd=0, cursor="hand2",
activebackground=self.mainColor,
command=self.root.destroy)
match case:
case 'test':
self.test()
return
case 'train':
self.train()
return
case 'predict':
self.predict()
return
def center(self, win, window_width, window_height):
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()
x_coordinate = int((screen_width / 2) - (window_width / 2))
y_coordinate = int((screen_height / 2) - (window_height / 2))
win.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")
def test(self):
pred = DTclf.predict(DTx_test)
joblib.dump(DTclf, "Decision_Tree_model.dt")
# Accuracy for testing
score = metrics.accuracy_score(DTy_test, y_pred=pred)
print("Accuracy Of ID3 Model(Test) :", score)
# -------------------------------------------------adding gui---------------------------------------------------
# Positions the window in the center of the page.
self.center(self.root, 700, 200)
frame = Frame(self.root, background=self.mainColor, pady=20)
global accuracyImage
accuracyImage = PhotoImage(file="Photos/Labels/testAccuracy.png")
accuracyLabel = Label(frame, image=accuracyImage, background=self.mainColor,
font=('Comic Sans MS', 20, 'bold'), foreground="#57d7ff")
accuracyValue = Label(frame, text=score, background=self.mainColor, font=('arial', 16, 'bold'),
foreground="white")
accuracyLabel.grid(row=0, column=0)
accuracyValue.grid(row=0, column=1)
frame.pack()
self.close_button.pack()
def train(self):
estimator = Id3Estimator()
estimator.fit(DTx_train, DTy_train)
# -------------------------------------------------adding gui---------------------------------------------------
# Positions the window in the center of the page.
self.center(self.root, 700, 200)
frame = Frame(self.root, background=self.mainColor, pady=20)
global successImage
successImage = PhotoImage(file="Photos/Labels/CreatedSuccessfully.png")
value = Label(frame, text="Model Created Successfully", image=successImage, background=self.mainColor,
font=('arial', 16, 'bold'),
foreground="white")
value.grid(row=0, column=1)
frame.pack()
self.close_button.pack()
def predict(self):
newData = pd.read_csv('model.csv')
newData = newData[['gender', 'SeniorCitizen', 'Partner', 'Dependents', 'tenure', 'PhoneService',
'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection',
'TechSupport', 'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling',
'PaymentMethod', 'MonthlyCharges', 'TotalCharges']]
# load saved model
model = joblib.load('cart_model.dt')
predictions = model.predict(newData)
print(f"predictions = {predictions}")
# -----------------------------------------------------adding gui-----------------------------------------------
# Positions the window in the center of the page.
self.center(self.root, 700, 200)
frame = Frame(self.root, background=self.mainColor, pady=20)
global predictionsImage
if predictions < 0.5:
predictionsImage = PhotoImage(file="Photos/Labels/customerWillKeepService.png")
self.accuracyValue = Label(frame, image=predictionsImage, background=self.mainColor,
font=('arial', 16, 'bold'),
foreground="white")
elif predictions >= 0.5:
predictionsImage = PhotoImage(file="Photos/Labels/customerCancelService.png")
self.accuracyValue = Label(frame, image=predictionsImage, background=self.mainColor,
font=('arial', 16, 'bold'),
foreground="white")
self.accuracyValue.grid(row=0, column=1)
frame.pack()
self.close_button.pack()