-
Notifications
You must be signed in to change notification settings - Fork 2
/
CARTClass.py
123 lines (100 loc) · 5.08 KB
/
CARTClass.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
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn import metrics
import joblib
import pandas as pd
from tkinter import *
class CARTClass:
def __init__(self, case):
self.treeData = pd.read_csv('Data.csv')
global treeX
treeX = self.treeData.drop('Churn', axis=1)
global treeY
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 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)
global DTx_train, DTx_test, DTy_train, DTy_test, clf, DTclf
DTx_train, DTx_test, DTy_train, DTy_test = train_test_split(treeX, treeY, test_size=0.50, random_state=101)
clf = tree.DecisionTreeClassifier(criterion="entropy")
DTclf = clf.fit(DTx_train, DTy_train)
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, "cart_model.dt")
score = metrics.accuracy_score(DTy_test, y_pred=pred)
print("Accuracy Of CART 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)
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):
clf = tree.DecisionTreeClassifier(criterion="entropy")
clf.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)
elif predictions >= 0.5:
predictionsImage = PhotoImage(file="Photos/Labels/customerCancelService.png")
self.accuracyValue = Label(frame, image=predictionsImage, background=self.mainColor)
self.accuracyValue.grid(row=0, column=1)
frame.pack()
self.close_button.pack()