TejAndrewsACC commited on
Commit
4157ea9
·
verified ·
1 Parent(s): 207db65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -204
app.py CHANGED
@@ -33,6 +33,9 @@ gpas = SPD.apply(grade_to_gpa, 'GradeClass')
33
 
34
  SPD = SPD.with_column('GPA', gpas)
35
 
 
 
 
36
  effectiveness_array = np.array(SPD.column('StudyEffectiveness'))
37
  gpa_array = np.array(SPD.column('GPA'))
38
 
@@ -136,6 +139,7 @@ shuffled = data.sample(with_replacement=False)
136
  size = int(data.num_rows * 0.8)
137
 
138
  train = shuffled.take(np.arange(size))
 
139
 
140
  def distance(r1, r2):
141
 
@@ -189,151 +193,64 @@ def knn_gpa(test_row, k):
189
 
190
  return round(gpa, 2)
191
 
192
- def random_forest_gpa(
193
- study_time,
194
- absences,
195
- parental_support,
196
- tutoring,
197
- extracurricular,
198
- sports,
199
- music,
200
- volunteering,
201
- effectiveness
202
- ):
203
-
204
- predictions = []
205
-
206
- np.random.seed(3)
207
-
208
- for i in range(30):
209
-
210
- sample = train.sample(k=train.num_rows, with_replacement=True)
211
-
212
- sample_effectiveness = np.mean(sample.column('StudyEffectiveness'))
213
- sample_gpa = np.mean(sample.column('GPA'))
214
-
215
- gpa = (
216
- (effectiveness / sample_effectiveness) *
217
- sample_gpa
218
- )
219
-
220
- gpa += (study_time * 0.015)
221
-
222
- gpa -= (absences * 0.01)
223
-
224
- gpa += (parental_support * 0.05)
225
-
226
- if tutoring:
227
- gpa += 0.15
228
-
229
- if extracurricular:
230
- gpa += 0.05
231
-
232
- if sports:
233
- gpa += 0.05
234
-
235
- if music:
236
- gpa += 0.05
237
-
238
- if volunteering:
239
- gpa += 0.05
240
-
241
- noise = np.random.normal(0, 0.08)
242
-
243
- gpa += noise
244
-
245
- if gpa < 0:
246
- gpa = 0
247
-
248
- if gpa > 4:
249
- gpa = 4
250
-
251
- predictions.append(gpa)
252
-
253
- return round(np.mean(predictions), 2)
254
-
255
  k = 5
256
 
257
- X = []
258
-
259
- for i in np.arange(SPD.num_rows):
260
-
261
- row = [
262
- SPD.column('StudyTimeWeekly').item(i) / 20,
263
- SPD.column('Absences').item(i) / 30,
264
- SPD.column('Tutoring').item(i),
265
- SPD.column('ParentalSupport').item(i) / 4,
266
- SPD.column('Extracurricular').item(i),
267
- SPD.column('Sports').item(i),
268
- SPD.column('Music').item(i),
269
- SPD.column('Volunteering').item(i),
270
- SPD.column('StudyEffectiveness').item(i) / 40
271
- ]
272
-
273
- X.append(row)
274
-
275
- X = np.array(X)
276
-
277
- y_gpa = np.array(SPD.column('GPA')).reshape(-1, 1) / 4
278
-
279
- np.random.seed(1)
280
-
281
- W1_gpa = np.random.normal(0, 1, (9, 16))
282
-
283
- W2_gpa = np.random.normal(0, 1, (16, 1))
284
-
285
- def sigmoid(x):
286
- return 1 / (1 + np.exp(-x))
287
-
288
- for i in range(20000):
289
-
290
- hidden = sigmoid(np.dot(X, W1_gpa))
291
-
292
- output = sigmoid(np.dot(hidden, W2_gpa))
293
-
294
- error = y_gpa - output
295
 
296
- output_change = error * output * (1 - output)
297
 
298
- hidden_error = np.dot(output_change, W2_gpa.T)
299
 
300
- hidden_change = hidden_error * hidden * (1 - hidden)
 
 
 
 
 
 
 
 
301
 
302
- W2_gpa = W2_gpa + 0.001 * np.dot(hidden.T, output_change)
303
-
304
- W1_gpa = W1_gpa + 0.001 * np.dot(X.T, hidden_change)
305
-
306
- def neural_network_gpa(x):
307
-
308
- hidden = sigmoid(np.dot(x, W1_gpa))
309
-
310
- output = sigmoid(np.dot(hidden, W2_gpa))
 
 
311
 
312
- gpa = float(output[0]) * 4
313
 
314
- if gpa < 0:
315
- gpa = 0
316
 
317
- if gpa > 4:
318
- gpa = 4
 
 
 
319
 
320
- return round(gpa, 2)
321
 
322
- def create_output(gpa):
323
 
324
- result = "PASS" if gpa >= 2.0 else "FAIL"
 
325
 
326
- return f"{result}\nGPA: {gpa}"
327
 
328
- def create_average_output(gpa):
329
 
330
- result = "PASS" if gpa >= 2.0 else "FAIL"
 
331
 
332
- return f"""
333
- FINAL RESULT: {result}
334
 
335
- AVERAGE GPA PREDICTION: {gpa}
336
- """
337
 
338
  def predict_models(
339
  outside_study_time,
@@ -373,18 +290,6 @@ def predict_models(
373
  study_effect
374
  )
375
 
376
- rf_gpa = random_forest_gpa(
377
- study_time,
378
- absences,
379
- parental_support,
380
- tutoring,
381
- extracurricular,
382
- sports,
383
- music,
384
- volunteering,
385
- study_effect
386
- )
387
-
388
  linear_gpa = linear_regression_gpa(
389
  study_effect
390
  )
@@ -408,38 +313,24 @@ def predict_models(
408
 
409
  knn_prediction = knn_gpa(test_row, k)
410
 
411
- x = np.array([
412
- study_time / 20,
413
- absences / 30,
414
- tutoring,
415
- parental_support / 4,
416
- extracurricular,
417
- sports,
418
- music,
419
- volunteering,
420
- study_effect / 40
421
  ])
422
 
423
- nn_prediction = neural_network_gpa(x)
424
-
425
- average_gpa = round(
426
- (
427
- tree_gpa +
428
- rf_gpa +
429
- linear_gpa +
430
- knn_prediction +
431
- nn_prediction
432
- ) / 5,
433
- 2
434
- )
435
 
436
  return (
437
- create_output(tree_gpa),
438
- create_output(rf_gpa),
439
- create_output(linear_gpa),
440
- create_output(knn_prediction),
441
- create_output(nn_prediction),
442
- create_average_output(average_gpa)
443
  )
444
 
445
  theme = gr.themes.Soft(
@@ -451,7 +342,6 @@ theme = gr.themes.Soft(
451
  )
452
 
453
  with gr.Blocks(
454
- theme=theme,
455
  fill_height=True
456
  ) as app:
457
 
@@ -459,7 +349,7 @@ with gr.Blocks(
459
  """
460
  # Student Performance Predictor
461
 
462
- ### Predict GPA and pass/fail results using multiple machine learning models
463
  """
464
  )
465
 
@@ -538,51 +428,36 @@ with gr.Blocks(
538
 
539
  tree_output = gr.Textbox(
540
  label="Decision Tree",
541
- lines=2,
542
- interactive=False,
543
- scale=1
544
  )
545
 
546
- rf_output = gr.Textbox(
547
- label="Random Forest",
548
- lines=2,
549
- interactive=False,
550
- scale=1
551
- )
552
-
553
- with gr.Row(equal_height=True):
554
-
555
  linear_output = gr.Textbox(
556
  label="Linear Regression",
557
- lines=2,
558
- interactive=False,
559
- scale=1
560
  )
561
 
 
 
562
  knn_output = gr.Textbox(
563
  label="KNN",
564
- lines=2,
565
- interactive=False,
566
- scale=1
567
  )
568
 
569
- with gr.Row(equal_height=True):
570
-
571
- nn_output = gr.Textbox(
572
- label="Neural Network",
573
- lines=2,
574
- interactive=False,
575
- scale=1
576
  )
577
 
578
  with gr.Row():
579
 
580
- average_output = gr.Textbox(
581
- label="Average Prediction",
582
- lines=5,
583
- interactive=False,
584
- max_lines=5,
585
- scale=1
586
  )
587
 
588
  btn.click(
@@ -601,11 +476,10 @@ with gr.Blocks(
601
  ],
602
  outputs=[
603
  tree_output,
604
- rf_output,
605
  linear_output,
606
  knn_output,
607
- nn_output,
608
- average_output
609
  ]
610
  )
611
 
 
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
 
 
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
 
 
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,
 
290
  study_effect
291
  )
292
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  linear_gpa = linear_regression_gpa(
294
  study_effect
295
  )
 
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(
 
342
  )
343
 
344
  with gr.Blocks(
 
345
  fill_height=True
346
  ) as app:
347
 
 
349
  """
350
  # Student Performance Predictor
351
 
352
+ ### Predict pass/fail results using multiple machine learning models
353
  """
354
  )
355
 
 
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(
 
476
  ],
477
  outputs=[
478
  tree_output,
 
479
  linear_output,
480
  knn_output,
481
+ final_output,
482
+ accuracy_box
483
  ]
484
  )
485