-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.py
More file actions
113 lines (90 loc) · 2.95 KB
/
DecisionTree.py
File metadata and controls
113 lines (90 loc) · 2.95 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
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.metrics import confusion_matrix,classification_report,ConfusionMatrixDisplay
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import plot_tree
file = 'KFC.xlsx'
sheet = 'Clean_data'
df = pd.read_excel(file, sheet_name=sheet)
st.header('Decision Tree Classification')
st.write('Prediction Budget Type')
le = LabelEncoder()
df['orderType_enc'] = le.fit_transform(df['orderType'].astype(str))
df['occupation_enc'] = le.fit_transform(df['occupation'].astype(str))
df_x = df[["orderType_enc","age_enc","occupation_enc"]]
df_y = df[[ "budget"]]
feature_list = df_x.columns
class_list = df_y [ "budget"].unique()
scaler = StandardScaler()
df_x_scaled = scaler.fit_transform(df_x)
#Showing Count and Target Class Count
st.subheader('Count')
st.metric(label="Total Count", value = len(df_y),)
class_list = df_y [ "budget"].dropna().unique()
for cls in class_list:
st.metric(label=cls,value=len(df_y[df_y[ "budget"]==cls]),)
#Number Input for Test Ratio
test_ratio = st.number_input("Select Ratio for Test Set", value = 0.2,)
#Setting The Test/Train Ratio
x_train, x_test, y_train, y_test = train_test_split(
df_x_scaled, df_y, test_size = test_ratio,random_state = 3,
)
tree = DecisionTreeClassifier(max_depth=3)
tree.fit (x_train, y_train)
#Predict and evaluate
y_pred = tree.predict(x_test)
#Predict and evaluate
y_pred = tree.predict(x_test)
st.write(f"Test Accuracy:{accuracy_score(y_test,y_pred):.2f}")
#Drawing Decision Tree
plt.clf()
plt.figure(figsize=(20,20))
plot_tree(tree, filled = True, feature_names = df_x.columns, class_names=df_y["budget"].unique())
st.pyplot(plt)
#Setup Input for Prediiction of Unknown Record
occupation_enc = st.selectbox(
"Occupation",
options=[0, 1, 2, 3],
format_func=lambda x: {
0: 'Others',
1: 'Staff',
2: 'Student',
3: 'TA'
}[x]
)
orderType_enc = st.selectbox(
"Order Type",
options=[0, 1, 2, 3],
format_func=lambda x: {
0: 'group',
1: 'individual',
2: 'promotion',
3: 'snack_sharing'
}[x]
)
age_enc =st.selectbox(
"Age",
options=[1, 2, 3, 4, 5],
format_func=lambda x: {
1: 'under 18',
2: '18-22',
3: '23-27',
4: '28-35',
5: 'above 35'
}[x]
)
#Predicting Class from Input
predict_data = pd.DataFrame({"Occupation":[occupation_enc],"Order Type":[orderType_enc],"Age":[age_enc]})
val = tree.predict(predict_data[["Occupation","Order Type","Age"]])[0]
accuracy = accuracy_score(y_test, y_pred)
st.write("Predicted Value:",val)
st.write("Test Accuracy",accuracy)