Spaces:
Sleeping
Sleeping
Gangsterbra123
commited on
Upload app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,25 @@ import pandas as pd
|
|
4 |
import ast
|
5 |
import numpy as np
|
6 |
import os
|
|
|
7 |
|
8 |
# Set the option to opt into future behavior
|
9 |
pd.set_option('future.no_silent_downcasting', True)
|
10 |
|
11 |
# List of options for the dropdown
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
sex_option = sorted(['Male', 'Female'])
|
19 |
age = [0, 100]
|
20 |
capital_gain = [0, 99999]
|
@@ -126,9 +134,43 @@ def Salary(model, workclass, education, marital_status, occupation, relationship
|
|
126 |
# Make predictions with the loaded model
|
127 |
prediction = loaded_model.predict(formattedDF)
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
salary_result = '<=50K' if prediction[0] == 0 else '>50K'
|
130 |
|
131 |
-
return f"Predicted using {model_used} Salary Class: {salary_result}"
|
132 |
|
133 |
def Health(model, age, sex, bmi, children, smoker, region):
|
134 |
|
@@ -199,7 +241,44 @@ def Health(model, age, sex, bmi, children, smoker, region):
|
|
199 |
prediction = loaded_model.predict(formattedDF)[0]
|
200 |
prediction = inverse_mapping_charges[prediction]
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
# interface one
|
205 |
iface1 = gr.Interface(
|
@@ -218,8 +297,9 @@ iface1 = gr.Interface(
|
|
218 |
gr.Slider(minimum=capital_loss[0], maximum=capital_loss[1], step=1, label="Capital Loss"),
|
219 |
gr.Slider(minimum=hours_per_week[0], maximum=hours_per_week[1], step=1, label="Hours per Week"),
|
220 |
],
|
221 |
-
outputs="
|
222 |
-
title="SVM - Salary"
|
|
|
223 |
)
|
224 |
|
225 |
# interface two
|
@@ -234,8 +314,9 @@ iface2 = gr.Interface(
|
|
234 |
gr.Dropdown(choices=smoker_option, label="Smoker"),
|
235 |
gr.Dropdown(choices=region_option, label="Region"),
|
236 |
],
|
237 |
-
outputs="
|
238 |
-
title="SVM - Health"
|
|
|
239 |
)
|
240 |
|
241 |
demo = gr.TabbedInterface([iface1, iface2], ["Salary Prediction", "Health Charges Prediction"])
|
|
|
4 |
import ast
|
5 |
import numpy as np
|
6 |
import os
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
|
9 |
# Set the option to opt into future behavior
|
10 |
pd.set_option('future.no_silent_downcasting', True)
|
11 |
|
12 |
# List of options for the dropdown
|
13 |
+
|
14 |
+
[("SVM - Jerome Agius", 0), ("Logistic Regression - Isaac Muscat", 1), ("Random Forest - Kyle Demicoli", 2)]
|
15 |
+
|
16 |
+
workclass_options = [('State Government', 'State-gov'),
|
17 |
+
('Self Employed Not Incorporated', 'Self-emp-not-inc'),
|
18 |
+
'Private', ('Federal Government', 'Federal-gov'), ('Local Government', 'Local-gov'), ('Self Employed Incorporated', 'Self-emp-inc'), ('Without Pay', 'Without-pay')]
|
19 |
+
|
20 |
+
education_option = [('Pre-School', 'Preschool'), '1st-4th', '5th-6th', '7th-8th', '9th', '10th', '11th', '12th', ('High School Graduate', 'HS-grad'), ('Collage', 'Some-college'), ('Associate Degree - Vocational', 'Assoc-voc'), ('Associate Degree - Academic', 'Assoc-acdm'), 'Bachelors', 'Masters', ('Professional School', 'Prof-school'), 'Doctorate']
|
21 |
+
|
22 |
+
marital_status_option = [('Never Married','Never-married'), ('Married Civilian Spouse', 'Married-civ-spouse'), 'Divorced', 'Separated', ('Married Armed Forces Spouse', 'Married-AF-spouse'), 'Widowed', ('Married Spouse Absent', 'Married-spouse-absent')]
|
23 |
+
occupation_option = [('Administrative Clerical', 'Adm-clerical'), ('Executive Managerial', 'Exec-managerial'), ('Handlers and Cleaners', 'Handlers-cleaners'), ('Professional Specialty', 'Prof-specialty'), 'Sales', ('Farming and Fishing', 'Farming-fishing'), ('Machine Operator and Inspector', 'Machine-op-inspct'), ('Other Service', 'Other-service'), ('Transport and Moving', 'Transport-moving'), ('Technical Support', 'Tech-support'), ('Craft and Repair', 'Craft-repair'), ('Protective Services', 'Protective-serv'), ('Armed Forces', 'Armed-Forces'), ('Private Household Services' ,'Priv-house-serv')]
|
24 |
+
relationship_option = [('Not In Family', 'Not-in-family'), 'Husband', 'Wife', ('Biological Child', 'Own-child'), 'Unmarried', ('Other Relative', 'Other-relative')]
|
25 |
+
race_option = ['White', 'Black', 'Other', ('Asian', 'Asian-Pac-Islander'), ('Indian', 'Amer-Indian-Eskimo')]
|
26 |
sex_option = sorted(['Male', 'Female'])
|
27 |
age = [0, 100]
|
28 |
capital_gain = [0, 99999]
|
|
|
134 |
# Make predictions with the loaded model
|
135 |
prediction = loaded_model.predict(formattedDF)
|
136 |
|
137 |
+
probability = loaded_model.predict_proba(formattedDF)
|
138 |
+
|
139 |
+
# Get the number of classes
|
140 |
+
num_classes = probability.shape[1]
|
141 |
+
|
142 |
+
class_dict = {
|
143 |
+
0: '<=50K',
|
144 |
+
1: '>50K'
|
145 |
+
}
|
146 |
+
|
147 |
+
# Select the probabilities for a single sample (e.g., the first sample)
|
148 |
+
probabilities = probability[0]
|
149 |
+
|
150 |
+
class_labels = [class_dict[i] for i in range(num_classes)]
|
151 |
+
colors = plt.cm.viridis(np.linspace(0, 1, num_classes)) # Use a colormap for consistent colors
|
152 |
+
|
153 |
+
fig, ax = plt.subplots(figsize=(10, 10))
|
154 |
+
_, _, autotexts = ax.pie(probabilities, colors=colors, autopct='%1.1f%%', startangle=140, pctdistance=1.1)
|
155 |
+
|
156 |
+
# Create a legend with colored boxes
|
157 |
+
legend_elements = []
|
158 |
+
for i, (color, label) in enumerate(zip(colors, class_labels)):
|
159 |
+
legend_elements.append(plt.Rectangle((0, 0), 1, 1, color=color, label=label))
|
160 |
+
|
161 |
+
ax.legend(handles=legend_elements, loc='upper left')
|
162 |
+
ax.set_title("Predicted Class Probabilities")
|
163 |
+
|
164 |
+
for i, p in enumerate(probabilities):
|
165 |
+
prob = float(round(p*100, 2))
|
166 |
+
if prob > 0:
|
167 |
+
autotexts[i].set_text(f"{prob}%")
|
168 |
+
else:
|
169 |
+
autotexts[i].set_text('')
|
170 |
+
|
171 |
salary_result = '<=50K' if prediction[0] == 0 else '>50K'
|
172 |
|
173 |
+
return f"Predicted using {model_used} Salary Class: {salary_result}", fig
|
174 |
|
175 |
def Health(model, age, sex, bmi, children, smoker, region):
|
176 |
|
|
|
241 |
prediction = loaded_model.predict(formattedDF)[0]
|
242 |
prediction = inverse_mapping_charges[prediction]
|
243 |
|
244 |
+
probability = loaded_model.predict_proba(formattedDF)
|
245 |
+
|
246 |
+
# Get the number of classes
|
247 |
+
num_classes = probability.shape[1]
|
248 |
+
|
249 |
+
class_dict = {
|
250 |
+
0: 'Very Low (<= 5000)',
|
251 |
+
1: 'Low (5001 - 10000)',
|
252 |
+
2: 'Moderate (10001 - 15000)',
|
253 |
+
3: 'High (15001 - 20000)',
|
254 |
+
4: 'Very High (> 20001)',
|
255 |
+
}
|
256 |
+
|
257 |
+
# Select the probabilities for a single sample (e.g., the first sample)
|
258 |
+
probabilities = probability[0]
|
259 |
+
|
260 |
+
class_labels = [class_dict[i] for i in range(num_classes)]
|
261 |
+
colors = plt.cm.viridis(np.linspace(0, 1, num_classes)) # Use a colormap for consistent colors
|
262 |
+
|
263 |
+
fig, ax = plt.subplots(figsize=(10, 10))
|
264 |
+
_, _, autotexts = ax.pie(probabilities, colors=colors, autopct='%1.1f%%', startangle=140, pctdistance=1.1)
|
265 |
+
|
266 |
+
# Create a legend with colored boxes
|
267 |
+
legend_elements = []
|
268 |
+
for i, (color, label) in enumerate(zip(colors, class_labels)):
|
269 |
+
legend_elements.append(plt.Rectangle((0, 0), 1, 1, color=color, label=label))
|
270 |
+
|
271 |
+
ax.legend(handles=legend_elements, loc='upper left')
|
272 |
+
ax.set_title("Predicted Class Probabilities")
|
273 |
+
|
274 |
+
for i, p in enumerate(probabilities):
|
275 |
+
prob = float(round(p*100, 2))
|
276 |
+
if prob > 0:
|
277 |
+
autotexts[i].set_text(f"{prob}%")
|
278 |
+
else:
|
279 |
+
autotexts[i].set_text('')
|
280 |
+
|
281 |
+
return f"Predicted using {model_used} Charges Class: {prediction}", fig
|
282 |
|
283 |
# interface one
|
284 |
iface1 = gr.Interface(
|
|
|
297 |
gr.Slider(minimum=capital_loss[0], maximum=capital_loss[1], step=1, label="Capital Loss"),
|
298 |
gr.Slider(minimum=hours_per_week[0], maximum=hours_per_week[1], step=1, label="Hours per Week"),
|
299 |
],
|
300 |
+
outputs=[gr.Text(label="Predicted Label"), gr.Plot(label="Predicted Class Probabilities")],
|
301 |
+
title="SVM - Salary",
|
302 |
+
flagging_mode="never"
|
303 |
)
|
304 |
|
305 |
# interface two
|
|
|
314 |
gr.Dropdown(choices=smoker_option, label="Smoker"),
|
315 |
gr.Dropdown(choices=region_option, label="Region"),
|
316 |
],
|
317 |
+
outputs=[gr.Text(label="Predicted Label"), gr.Plot(label="Predicted Class Probabilities")],
|
318 |
+
title="SVM - Health",
|
319 |
+
flagging_mode="never"
|
320 |
)
|
321 |
|
322 |
demo = gr.TabbedInterface([iface1, iface2], ["Salary Prediction", "Health Charges Prediction"])
|