TejAndrewsACC commited on
Commit
509debf
·
verified ·
1 Parent(s): 192c327

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -219
app.py CHANGED
@@ -30,12 +30,8 @@ def grade_to_gpa(grade):
30
  return 0.0
31
 
32
  gpas = SPD.apply(grade_to_gpa, 'GradeClass')
33
-
34
  SPD = SPD.with_column('GPA', gpas)
35
 
36
- def gpa_to_label(gpa):
37
- return "PASS" if gpa >= 2.0 else "FAIL"
38
-
39
  effectiveness_array = np.array(SPD.column('StudyEffectiveness'))
40
  gpa_array = np.array(SPD.column('GPA'))
41
 
@@ -53,15 +49,11 @@ slope = (
53
  intercept = np.mean(gpa_array) - (slope * np.mean(effectiveness_array))
54
 
55
  def linear_regression_gpa(effectiveness):
56
-
57
  gpa = intercept + (slope * effectiveness)
58
-
59
  if gpa < 0:
60
  gpa = 0
61
-
62
  if gpa > 4:
63
  gpa = 4
64
-
65
  return round(gpa, 2)
66
 
67
  def predict_tree_gpa(
@@ -75,16 +67,13 @@ def predict_tree_gpa(
75
  volunteering,
76
  effectiveness
77
  ):
78
-
79
  if effectiveness >= 18:
80
  gpa = 3.6 + (study_time * 0.02) - (absences * 0.01)
81
-
82
  elif effectiveness >= 14:
83
  if absences <= 18:
84
  gpa = 2.8 + (study_time * 0.03) - (absences * 0.015)
85
  else:
86
  gpa = 1.7 - (absences * 0.02)
87
-
88
  elif effectiveness >= 10:
89
  if study_time >= 10 and absences <= 12:
90
  gpa = 2.4 + (study_time * 0.03)
@@ -92,7 +81,6 @@ def predict_tree_gpa(
92
  gpa = 2.2 + (parental_support * 0.08)
93
  else:
94
  gpa = 1.5 - (absences * 0.02)
95
-
96
  elif effectiveness >= 6:
97
  if study_time >= 12 and absences < 8:
98
  gpa = 2.1 + (study_time * 0.025)
@@ -103,16 +91,13 @@ def predict_tree_gpa(
103
  gpa = 1.3
104
  else:
105
  gpa = 1.0
106
-
107
  else:
108
  gpa = 0.6
109
 
110
  if gpa < 0:
111
  gpa = 0
112
-
113
  if gpa > 4:
114
  gpa = 4
115
-
116
  return round(gpa, 2)
117
 
118
  data = SPD.select(
@@ -133,18 +118,12 @@ data = SPD.select(
133
  )
134
 
135
  np.random.seed(1)
136
-
137
  shuffled = data.sample(with_replacement=False)
138
-
139
  size = int(data.num_rows * 0.8)
140
-
141
  train = shuffled.take(np.arange(size))
142
- test = shuffled.take(np.arange(size, data.num_rows))
143
 
144
  def distance(r1, r2):
145
-
146
  total = 0
147
-
148
  total += (r1.item('Age') - r2.item('Age'))**2
149
  total += (r1.item('Gender') - r2.item('Gender'))**2
150
  total += (r1.item('Ethnicity') - r2.item('Ethnicity'))**2
@@ -158,100 +137,29 @@ def distance(r1, r2):
158
  total += (r1.item('Music') - r2.item('Music'))**2
159
  total += (r1.item('Volunteering') - r2.item('Volunteering'))**2
160
  total += (r1.item('StudyEffectiveness') - r2.item('StudyEffectiveness'))**2
161
-
162
  return np.sqrt(total)
163
 
164
  def knn_neighbors(test_row, k):
165
-
166
  dists = make_array()
167
-
168
  for i in np.arange(train.num_rows):
169
-
170
  row = train.row(i)
171
-
172
  d = distance(test_row, row)
173
-
174
  dists = np.append(dists, d)
175
-
176
  temp = train.with_column('Distance', dists)
177
-
178
  nearest = temp.sort('Distance').take(np.arange(k))
179
-
180
  return nearest
181
 
182
  def knn_gpa(test_row, k):
183
-
184
  nearest = knn_neighbors(test_row, k)
185
-
186
  gpa = np.mean(nearest.column('GPA'))
187
-
188
  if gpa < 0:
189
  gpa = 0
190
-
191
  if gpa > 4:
192
  gpa = 4
193
-
194
  return round(gpa, 2)
195
 
196
  k = 5
197
 
198
- correct = 0
199
-
200
- for i in np.arange(test.num_rows):
201
-
202
- row = test.row(i)
203
-
204
- study_time = row.item('StudyTimeWeekly')
205
- absences = row.item('Absences')
206
- parental_support = row.item('ParentalSupport')
207
- tutoring = row.item('Tutoring')
208
- extracurricular = row.item('Extracurricular')
209
- sports = row.item('Sports')
210
- music = row.item('Music')
211
- volunteering = row.item('Volunteering')
212
- effectiveness = row.item('StudyEffectiveness')
213
-
214
- tree_gpa = predict_tree_gpa(
215
- study_time,
216
- absences,
217
- parental_support,
218
- tutoring,
219
- extracurricular,
220
- sports,
221
- music,
222
- volunteering,
223
- effectiveness
224
- )
225
-
226
- linear_gpa = linear_regression_gpa(effectiveness)
227
-
228
- knn_prediction = knn_gpa(row, k)
229
-
230
- predictions = [
231
- gpa_to_label(tree_gpa),
232
- gpa_to_label(linear_gpa),
233
- gpa_to_label(knn_prediction)
234
- ]
235
-
236
- final_prediction = max(set(predictions), key=predictions.count)
237
-
238
- actual = gpa_to_label(row.item('GPA'))
239
-
240
- if final_prediction == actual:
241
- correct += 1
242
-
243
- ensemble_accuracy = round(correct / test.num_rows, 4)
244
-
245
- def majority_vote(predictions):
246
-
247
- passes = predictions.count("PASS")
248
- fails = predictions.count("FAIL")
249
-
250
- if passes > fails:
251
- return "PASS"
252
-
253
- return "FAIL"
254
-
255
  def predict_models(
256
  outside_study_time,
257
  in_class_learning_time,
@@ -264,12 +172,9 @@ def predict_models(
264
  music,
265
  volunteering
266
  ):
267
-
268
  attention_multiplier = attentiveness / 10
269
 
270
- study_time = outside_study_time + (
271
- in_class_learning_time * attention_multiplier
272
- )
273
 
274
  study_effect = (
275
  (study_time * 1.5)
@@ -290,9 +195,7 @@ def predict_models(
290
  study_effect
291
  )
292
 
293
- linear_gpa = linear_regression_gpa(
294
- study_effect
295
- )
296
 
297
  test_row = Table().with_columns(
298
  'Age', [17],
@@ -313,25 +216,10 @@ def predict_models(
313
 
314
  knn_prediction = knn_gpa(test_row, k)
315
 
316
- tree_label = gpa_to_label(tree_gpa)
317
- linear_label = gpa_to_label(linear_gpa)
318
- knn_label = gpa_to_label(knn_prediction)
319
 
320
- final_prediction = majority_vote([
321
- tree_label,
322
- linear_label,
323
- knn_label
324
- ])
325
-
326
- accuracy_output = f"Combined Model Accuracy: {ensemble_accuracy}"
327
-
328
- return (
329
- tree_label,
330
- linear_label,
331
- knn_label,
332
- final_prediction,
333
- accuracy_output
334
- )
335
 
336
  theme = gr.themes.Soft(
337
  primary_hue="blue",
@@ -341,15 +229,13 @@ theme = gr.themes.Soft(
341
  text_size="lg"
342
  )
343
 
344
- with gr.Blocks(
345
- fill_height=True
346
- ) as app:
347
 
348
  gr.Markdown(
349
  """
350
  # Student Performance Predictor
351
 
352
- ### Predict pass/fail results using multiple machine learning models
353
  """
354
  )
355
 
@@ -359,106 +245,35 @@ with gr.Blocks(
359
 
360
  with gr.Group():
361
 
362
- outside_study_time = gr.Slider(
363
- 0,
364
- 20,
365
- value=8,
366
- label="Study Time Outside Class Weekly"
367
- )
368
-
369
- in_class_learning_time = gr.Slider(
370
- 0,
371
- 25,
372
- value=15,
373
- label="Learning Time In Class Weekly"
374
- )
375
-
376
- attentiveness = gr.Slider(
377
- 1,
378
- 10,
379
- value=5,
380
- step=1,
381
- label="Attentiveness In Class"
382
- )
383
-
384
- absences = gr.Slider(
385
- 0,
386
- 30,
387
- value=5,
388
- label="Absences"
389
- )
390
-
391
- parental_support = gr.Slider(
392
- 0,
393
- 4,
394
- value=2,
395
- step=1,
396
- label="Parental Support"
397
- )
398
-
399
- tutoring = gr.Checkbox(
400
- label="Tutoring"
401
- )
402
-
403
- extracurricular = gr.Checkbox(
404
- label="Extracurricular Activities"
405
- )
406
-
407
- sports = gr.Checkbox(
408
- label="Sports"
409
- )
410
-
411
- music = gr.Checkbox(
412
- label="Music"
413
- )
414
-
415
- volunteering = gr.Checkbox(
416
- label="Volunteering"
417
- )
418
-
419
- btn = gr.Button(
420
- "Predict Performance",
421
- variant="primary",
422
- size="lg"
423
- )
424
 
425
- with gr.Column(scale=1):
426
 
427
- with gr.Row(equal_height=True):
428
 
429
- tree_output = gr.Textbox(
430
- label="Decision Tree",
431
- lines=1,
432
- interactive=False
433
- )
434
 
435
- linear_output = gr.Textbox(
436
- label="Linear Regression",
437
- lines=1,
438
- interactive=False
439
- )
440
 
441
- with gr.Row(equal_height=True):
442
 
443
- knn_output = gr.Textbox(
444
- label="KNN",
445
- lines=1,
446
- interactive=False
447
- )
448
 
449
- final_output = gr.Textbox(
450
- label="Final Majority Vote",
451
- lines=1,
452
- interactive=False
453
- )
454
 
455
- with gr.Row():
 
 
 
 
 
 
456
 
457
- accuracy_box = gr.Textbox(
458
- label="Combined Model Accuracy",
459
- lines=1,
460
- interactive=False
461
- )
462
 
463
  btn.click(
464
  predict_models,
@@ -474,13 +289,7 @@ with gr.Blocks(
474
  music,
475
  volunteering
476
  ],
477
- outputs=[
478
- tree_output,
479
- linear_output,
480
- knn_output,
481
- final_output,
482
-
483
- ]
484
  )
485
 
486
  app.launch()
 
30
  return 0.0
31
 
32
  gpas = SPD.apply(grade_to_gpa, 'GradeClass')
 
33
  SPD = SPD.with_column('GPA', gpas)
34
 
 
 
 
35
  effectiveness_array = np.array(SPD.column('StudyEffectiveness'))
36
  gpa_array = np.array(SPD.column('GPA'))
37
 
 
49
  intercept = np.mean(gpa_array) - (slope * np.mean(effectiveness_array))
50
 
51
  def linear_regression_gpa(effectiveness):
 
52
  gpa = intercept + (slope * effectiveness)
 
53
  if gpa < 0:
54
  gpa = 0
 
55
  if gpa > 4:
56
  gpa = 4
 
57
  return round(gpa, 2)
58
 
59
  def predict_tree_gpa(
 
67
  volunteering,
68
  effectiveness
69
  ):
 
70
  if effectiveness >= 18:
71
  gpa = 3.6 + (study_time * 0.02) - (absences * 0.01)
 
72
  elif effectiveness >= 14:
73
  if absences <= 18:
74
  gpa = 2.8 + (study_time * 0.03) - (absences * 0.015)
75
  else:
76
  gpa = 1.7 - (absences * 0.02)
 
77
  elif effectiveness >= 10:
78
  if study_time >= 10 and absences <= 12:
79
  gpa = 2.4 + (study_time * 0.03)
 
81
  gpa = 2.2 + (parental_support * 0.08)
82
  else:
83
  gpa = 1.5 - (absences * 0.02)
 
84
  elif effectiveness >= 6:
85
  if study_time >= 12 and absences < 8:
86
  gpa = 2.1 + (study_time * 0.025)
 
91
  gpa = 1.3
92
  else:
93
  gpa = 1.0
 
94
  else:
95
  gpa = 0.6
96
 
97
  if gpa < 0:
98
  gpa = 0
 
99
  if gpa > 4:
100
  gpa = 4
 
101
  return round(gpa, 2)
102
 
103
  data = SPD.select(
 
118
  )
119
 
120
  np.random.seed(1)
 
121
  shuffled = data.sample(with_replacement=False)
 
122
  size = int(data.num_rows * 0.8)
 
123
  train = shuffled.take(np.arange(size))
 
124
 
125
  def distance(r1, r2):
 
126
  total = 0
 
127
  total += (r1.item('Age') - r2.item('Age'))**2
128
  total += (r1.item('Gender') - r2.item('Gender'))**2
129
  total += (r1.item('Ethnicity') - r2.item('Ethnicity'))**2
 
137
  total += (r1.item('Music') - r2.item('Music'))**2
138
  total += (r1.item('Volunteering') - r2.item('Volunteering'))**2
139
  total += (r1.item('StudyEffectiveness') - r2.item('StudyEffectiveness'))**2
 
140
  return np.sqrt(total)
141
 
142
  def knn_neighbors(test_row, k):
 
143
  dists = make_array()
 
144
  for i in np.arange(train.num_rows):
 
145
  row = train.row(i)
 
146
  d = distance(test_row, row)
 
147
  dists = np.append(dists, d)
 
148
  temp = train.with_column('Distance', dists)
 
149
  nearest = temp.sort('Distance').take(np.arange(k))
 
150
  return nearest
151
 
152
  def knn_gpa(test_row, k):
 
153
  nearest = knn_neighbors(test_row, k)
 
154
  gpa = np.mean(nearest.column('GPA'))
 
155
  if gpa < 0:
156
  gpa = 0
 
157
  if gpa > 4:
158
  gpa = 4
 
159
  return round(gpa, 2)
160
 
161
  k = 5
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  def predict_models(
164
  outside_study_time,
165
  in_class_learning_time,
 
172
  music,
173
  volunteering
174
  ):
 
175
  attention_multiplier = attentiveness / 10
176
 
177
+ study_time = outside_study_time + (in_class_learning_time * attention_multiplier)
 
 
178
 
179
  study_effect = (
180
  (study_time * 1.5)
 
195
  study_effect
196
  )
197
 
198
+ linear_gpa = linear_regression_gpa(study_effect)
 
 
199
 
200
  test_row = Table().with_columns(
201
  'Age', [17],
 
216
 
217
  knn_prediction = knn_gpa(test_row, k)
218
 
219
+ avg_gpa = (tree_gpa + linear_gpa + knn_prediction) / 3
 
 
220
 
221
+ result = "PASS" if avg_gpa >= 2.0 else "FAIL"
222
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  theme = gr.themes.Soft(
225
  primary_hue="blue",
 
229
  text_size="lg"
230
  )
231
 
232
+ with gr.Blocks(theme=theme, fill_height=True) as app:
 
 
233
 
234
  gr.Markdown(
235
  """
236
  # Student Performance Predictor
237
 
238
+ ### Predict Pass or Fail using machine learning models
239
  """
240
  )
241
 
 
245
 
246
  with gr.Group():
247
 
248
+ outside_study_time = gr.Slider(0, 20, value=8, label="Study Time Outside Class Weekly")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
+ in_class_learning_time = gr.Slider(0, 25, value=15, label="Learning Time In Class Weekly")
251
 
252
+ attentiveness = gr.Slider(1, 10, value=5, step=1, label="Attentiveness In Class")
253
 
254
+ absences = gr.Slider(0, 30, value=5, label="Absences")
 
 
 
 
255
 
256
+ parental_support = gr.Slider(0, 4, value=2, step=1, label="Parental Support")
 
 
 
 
257
 
258
+ tutoring = gr.Checkbox(label="Tutoring")
259
 
260
+ extracurricular = gr.Checkbox(label="Extracurricular Activities")
 
 
 
 
261
 
262
+ sports = gr.Checkbox(label="Sports")
 
 
 
 
263
 
264
+ music = gr.Checkbox(label="Music")
265
+
266
+ volunteering = gr.Checkbox(label="Volunteering")
267
+
268
+ btn = gr.Button("Predict Performance", variant="primary", size="lg")
269
+
270
+ with gr.Column(scale=1):
271
 
272
+ result_output = gr.Textbox(
273
+ label="Result",
274
+ lines=3,
275
+ interactive=False
276
+ )
277
 
278
  btn.click(
279
  predict_models,
 
289
  music,
290
  volunteering
291
  ],
292
+ outputs=[result_output]
 
 
 
 
 
 
293
  )
294
 
295
  app.launch()