TejAndrewsACC commited on
Commit
cd94b7b
·
verified ·
1 Parent(s): bc41b18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +344 -36
app.py CHANGED
@@ -4,6 +4,10 @@ import gradio as gr
4
 
5
  SPD = Table.read_table('Student_Performance_Data.csv')
6
 
 
 
 
 
7
  def study_effectiveness(study_time, absences, parental_support, tutoring):
8
  return (study_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
9
 
@@ -17,6 +21,166 @@ effectiveness_scores = SPD.apply(
17
 
18
  SPD = SPD.with_column('StudyEffectiveness', effectiveness_scores)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def pass_or_fail(grade):
21
  if grade == 4:
22
  return 0
@@ -29,7 +193,7 @@ SPD = SPD.with_column('Pass', passes)
29
  X = []
30
 
31
  for i in np.arange(SPD.num_rows):
32
- X.append([
33
  SPD.column('StudyTimeWeekly').item(i) / 20,
34
  SPD.column('Absences').item(i) / 30,
35
  SPD.column('Tutoring').item(i),
@@ -39,67 +203,186 @@ for i in np.arange(SPD.num_rows):
39
  SPD.column('Music').item(i),
40
  SPD.column('Volunteering').item(i),
41
  SPD.column('StudyEffectiveness').item(i) / 40
42
- ])
 
43
 
44
  X = np.array(X)
45
  y = np.array(SPD.column('Pass'))
46
 
47
  np.random.seed(1)
 
48
  W1 = np.random.normal(0, 1, (9, 16))
49
  W2 = np.random.normal(0, 1, (16, 1))
50
 
 
51
  def sigmoid(x):
52
  return 1 / (1 + np.exp(-x))
53
 
 
54
  for i in range(20000):
55
  hidden = sigmoid(np.dot(X, W1))
56
  output = sigmoid(np.dot(hidden, W2))
 
57
  error = y.reshape(len(y), 1) - output
 
58
  output_change = error * output * (1 - output)
 
59
  hidden_error = np.dot(output_change, W2.T)
60
  hidden_change = hidden_error * hidden * (1 - hidden)
61
- W2 += 0.001 * np.dot(hidden.T, output_change)
62
- W1 += 0.001 * np.dot(X.T, hidden_change)
 
63
 
64
  hidden = sigmoid(np.dot(X, W1))
65
  output = sigmoid(np.dot(hidden, W2))
 
66
  predictions = output > 0.5
67
- accuracy_nn = np.mean(predictions.flatten() == y)
68
-
69
- def predict(student_time, absences, tutoring, parental_support, extracurricular, sports, music, volunteering):
70
- study_effect = (student_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
71
-
72
- x = np.array([
73
- student_time / 20,
74
- absences / 30,
75
- tutoring,
76
- parental_support / 4,
77
- extracurricular,
78
- sports,
79
- music,
80
- volunteering,
81
- study_effect / 40
82
- ])
83
-
84
- hidden = sigmoid(np.dot(x, W1))
85
- output = sigmoid(np.dot(hidden, W2))
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- result = "PASS" if output[0] > 0.5 else "FAIL"
88
- confidence = float(output[0])
 
 
 
 
 
 
 
 
 
89
 
90
- return result, round(confidence, 4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  theme = gr.themes.Soft()
93
 
94
  with gr.Blocks(theme=theme) as app:
 
95
  gr.Markdown("# Student Performance Predictor")
96
- gr.Markdown("Predict whether a student will pass or fail based on academic behavior and activities.")
 
 
 
97
 
98
  with gr.Row():
 
99
  with gr.Column():
100
- student_time = gr.Slider(0, 20, value=10, label="Study Time Weekly")
101
- absences = gr.Slider(0, 30, value=5, label="Absences")
102
- parental_support = gr.Slider(0, 4, value=2, step=1, label="Parental Support")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  tutoring = gr.Checkbox(label="Tutoring")
105
  extracurricular = gr.Checkbox(label="Extracurricular Activities")
@@ -107,18 +390,43 @@ with gr.Blocks(theme=theme) as app:
107
  music = gr.Checkbox(label="Music")
108
  volunteering = gr.Checkbox(label="Volunteering")
109
 
110
- btn = gr.Button("Predict", variant="primary")
 
 
 
111
 
112
  with gr.Column():
 
113
  result = gr.Textbox(label="Prediction")
114
- confidence = gr.Number(label="Confidence Score")
 
 
 
115
 
116
  btn.click(
117
- predict,
118
- inputs=[student_time, absences, tutoring, parental_support, extracurricular, sports, music, volunteering],
119
- outputs=[result, confidence]
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  )
121
 
122
- gr.Markdown(f"Model training accuracy: {accuracy_nn:.3f}")
 
 
 
 
 
123
 
124
  app.launch()
 
4
 
5
  SPD = Table.read_table('Student_Performance_Data.csv')
6
 
7
+ # =========================
8
+ # SHARED FEATURE ENGINEERING
9
+ # =========================
10
+
11
  def study_effectiveness(study_time, absences, parental_support, tutoring):
12
  return (study_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
13
 
 
21
 
22
  SPD = SPD.with_column('StudyEffectiveness', effectiveness_scores)
23
 
24
+ # =========================================================
25
+ # DECISION TREE MODEL (EXACT CODE YOU PROVIDED)
26
+ # =========================================================
27
+
28
+ def predict_pass(study_time, absences, parental_support, tutoring, extracurricular, sports, music, volunteering, effectiveness):
29
+
30
+ if effectiveness >= 18:
31
+ return True
32
+
33
+ elif effectiveness >= 14:
34
+ if absences <= 18:
35
+ return True
36
+ else:
37
+ return False
38
+
39
+ elif effectiveness >= 10:
40
+ if study_time >= 10 and absences <= 12:
41
+ return True
42
+ elif tutoring == 1 and parental_support >= 3:
43
+ return True
44
+ else:
45
+ return False
46
+
47
+ elif effectiveness >= 6:
48
+ if study_time >= 12 and absences < 8:
49
+ return True
50
+ elif extracurricular == 1 or sports == 1 or music == 1 or volunteering == 1:
51
+ if absences < 12 and parental_support >= 2:
52
+ return True
53
+ else:
54
+ return False
55
+ else:
56
+ return False
57
+
58
+ else:
59
+ return False
60
+
61
+
62
+ predicted = SPD.apply(
63
+ predict_pass,
64
+ 'StudyTimeWeekly',
65
+ 'Absences',
66
+ 'ParentalSupport',
67
+ 'Tutoring',
68
+ 'Extracurricular',
69
+ 'Sports',
70
+ 'Music',
71
+ 'Volunteering',
72
+ 'StudyEffectiveness'
73
+ )
74
+
75
+
76
+ def actual_pass(grade):
77
+ if grade == 4:
78
+ return False
79
+ else:
80
+ return True
81
+
82
+
83
+ true_labels = SPD.apply(actual_pass, 'GradeClass')
84
+
85
+ correct = predicted == true_labels
86
+ accuracy_tree = sum(correct) / len(correct)
87
+
88
+ SPD = SPD.with_columns(
89
+ 'predicted_pass', predicted,
90
+ 'true_pass', true_labels,
91
+ 'correct', correct
92
+ )
93
+
94
+ # =========================================================
95
+ # KNN MODEL (EXACT CODE YOU PROVIDED)
96
+ # =========================================================
97
+
98
+ def pass_fail(x):
99
+ if x == 4:
100
+ return 0
101
+ else:
102
+ return 1
103
+
104
+ passes_knn = SPD.apply(pass_fail, 'GradeClass')
105
+ SPD = SPD.with_column('PassFail', passes_knn)
106
+
107
+ data = SPD.select(
108
+ 'Age',
109
+ 'Gender',
110
+ 'Ethnicity',
111
+ 'ParentalEducation',
112
+ 'StudyTimeWeekly',
113
+ 'Absences',
114
+ 'Tutoring',
115
+ 'ParentalSupport',
116
+ 'Extracurricular',
117
+ 'Sports',
118
+ 'Music',
119
+ 'Volunteering',
120
+ 'StudyEffectiveness',
121
+ 'PassFail'
122
+ )
123
+
124
+ np.random.seed(1)
125
+ shuffled = data.sample(with_replacement=False)
126
+ size = int(data.num_rows * 0.8)
127
+
128
+ train = shuffled.take(np.arange(size))
129
+ test = shuffled.take(np.arange(size, data.num_rows))
130
+
131
+
132
+ def distance(r1, r2):
133
+ total = 0
134
+ total += (r1.item('Age') - r2.item('Age'))**2
135
+ total += (r1.item('Gender') - r2.item('Gender'))**2
136
+ total += (r1.item('Ethnicity') - r2.item('Ethnicity'))**2
137
+ total += (r1.item('ParentalEducation') - r2.item('ParentalEducation'))**2
138
+ total += (r1.item('StudyTimeWeekly') - r2.item('StudyTimeWeekly'))**2
139
+ total += (r1.item('Absences') - r2.item('Absences'))**2
140
+ total += (r1.item('Tutoring') - r2.item('Tutoring'))**2
141
+ total += (r1.item('ParentalSupport') - r2.item('ParentalSupport'))**2
142
+ total += (r1.item('Extracurricular') - r2.item('Extracurricular'))**2
143
+ total += (r1.item('Sports') - r2.item('Sports'))**2
144
+ total += (r1.item('Music') - r2.item('Music'))**2
145
+ total += (r1.item('Volunteering') - r2.item('Volunteering'))**2
146
+ total += (r1.item('StudyEffectiveness') - r2.item('StudyEffectiveness'))**2
147
+ return np.sqrt(total)
148
+
149
+
150
+ def knn(test_row, k):
151
+ dists = make_array()
152
+
153
+ for i in np.arange(train.num_rows):
154
+ row = train.row(i)
155
+ d = distance(test_row, row)
156
+ dists = np.append(dists, d)
157
+
158
+ temp = train.with_column('Distance', dists)
159
+ nearest = temp.sort('Distance').take(np.arange(k))
160
+ total = np.sum(nearest.column('PassFail'))
161
+
162
+ if total > k / 2:
163
+ return 1
164
+ else:
165
+ return 0
166
+
167
+
168
+ k = 5
169
+
170
+ predictions_knn = make_array()
171
+
172
+ for i in np.arange(test.num_rows):
173
+ row = test.row(i)
174
+ p = knn(row, k)
175
+ predictions_knn = np.append(predictions_knn, p)
176
+
177
+ actual = test.column('PassFail')
178
+ accuracy_knn = np.mean(predictions_knn == actual)
179
+
180
+ # =========================================================
181
+ # NEURAL NETWORK MODEL (EXACT CODE YOU PROVIDED)
182
+ # =========================================================
183
+
184
  def pass_or_fail(grade):
185
  if grade == 4:
186
  return 0
 
193
  X = []
194
 
195
  for i in np.arange(SPD.num_rows):
196
+ row = [
197
  SPD.column('StudyTimeWeekly').item(i) / 20,
198
  SPD.column('Absences').item(i) / 30,
199
  SPD.column('Tutoring').item(i),
 
203
  SPD.column('Music').item(i),
204
  SPD.column('Volunteering').item(i),
205
  SPD.column('StudyEffectiveness').item(i) / 40
206
+ ]
207
+ X.append(row)
208
 
209
  X = np.array(X)
210
  y = np.array(SPD.column('Pass'))
211
 
212
  np.random.seed(1)
213
+
214
  W1 = np.random.normal(0, 1, (9, 16))
215
  W2 = np.random.normal(0, 1, (16, 1))
216
 
217
+
218
  def sigmoid(x):
219
  return 1 / (1 + np.exp(-x))
220
 
221
+
222
  for i in range(20000):
223
  hidden = sigmoid(np.dot(X, W1))
224
  output = sigmoid(np.dot(hidden, W2))
225
+
226
  error = y.reshape(len(y), 1) - output
227
+
228
  output_change = error * output * (1 - output)
229
+
230
  hidden_error = np.dot(output_change, W2.T)
231
  hidden_change = hidden_error * hidden * (1 - hidden)
232
+
233
+ W2 = W2 + 0.001 * np.dot(hidden.T, output_change)
234
+ W1 = W1 + 0.001 * np.dot(X.T, hidden_change)
235
 
236
  hidden = sigmoid(np.dot(X, W1))
237
  output = sigmoid(np.dot(hidden, W2))
238
+
239
  predictions = output > 0.5
240
+ correct = predictions.flatten() == y
241
+
242
+ accuracy_nn = np.mean(correct)
243
+
244
+ # =========================================================
245
+ # GRADIO PREDICTION FUNCTION
246
+ # =========================================================
247
+
248
+ def predict_model(
249
+ model_choice,
250
+ student_time,
251
+ absences,
252
+ tutoring,
253
+ parental_support,
254
+ extracurricular,
255
+ sports,
256
+ music,
257
+ volunteering
258
+ ):
259
+
260
+ study_effect = (
261
+ (student_time * 1.5)
262
+ + (parental_support * 2)
263
+ + (tutoring * 3)
264
+ - (absences * 0.7)
265
+ )
266
+
267
+ # =====================
268
+ # DECISION TREE
269
+ # =====================
270
+ if model_choice == "Decision Tree":
271
 
272
+ result = predict_pass(
273
+ student_time,
274
+ absences,
275
+ parental_support,
276
+ tutoring,
277
+ extracurricular,
278
+ sports,
279
+ music,
280
+ volunteering,
281
+ study_effect
282
+ )
283
 
284
+ prediction = "PASS" if result else "FAIL"
285
+
286
+ return prediction, round(float(accuracy_tree), 4)
287
+
288
+ # =====================
289
+ # KNN
290
+ # =====================
291
+ elif model_choice == "KNN":
292
+
293
+ test_row = Table().with_columns(
294
+ 'Age', [17],
295
+ 'Gender', [0],
296
+ 'Ethnicity', [0],
297
+ 'ParentalEducation', [2],
298
+ 'StudyTimeWeekly', [student_time],
299
+ 'Absences', [absences],
300
+ 'Tutoring', [int(tutoring)],
301
+ 'ParentalSupport', [parental_support],
302
+ 'Extracurricular', [int(extracurricular)],
303
+ 'Sports', [int(sports)],
304
+ 'Music', [int(music)],
305
+ 'Volunteering', [int(volunteering)],
306
+ 'StudyEffectiveness', [study_effect],
307
+ 'PassFail', [0]
308
+ ).row(0)
309
+
310
+ result = knn(test_row, k)
311
+
312
+ prediction = "PASS" if result == 1 else "FAIL"
313
+
314
+ return prediction, round(float(accuracy_knn), 4)
315
+
316
+ # =====================
317
+ # NEURAL NETWORK
318
+ # =====================
319
+ elif model_choice == "Neural Network":
320
+
321
+ x = np.array([
322
+ student_time / 20,
323
+ absences / 30,
324
+ tutoring,
325
+ parental_support / 4,
326
+ extracurricular,
327
+ sports,
328
+ music,
329
+ volunteering,
330
+ study_effect / 40
331
+ ])
332
+
333
+ hidden = sigmoid(np.dot(x, W1))
334
+ output = sigmoid(np.dot(hidden, W2))
335
+
336
+ prediction = "PASS" if output[0] > 0.5 else "FAIL"
337
+
338
+ return prediction, round(float(output[0]), 4)
339
+
340
+ # =========================================================
341
+ # GRADIO UI
342
+ # =========================================================
343
 
344
  theme = gr.themes.Soft()
345
 
346
  with gr.Blocks(theme=theme) as app:
347
+
348
  gr.Markdown("# Student Performance Predictor")
349
+
350
+ gr.Markdown(
351
+ "Select a machine learning model and predict whether a student will pass or fail."
352
+ )
353
 
354
  with gr.Row():
355
+
356
  with gr.Column():
357
+
358
+ model_choice = gr.Dropdown(
359
+ choices=[
360
+ "Decision Tree",
361
+ "KNN",
362
+ "Neural Network"
363
+ ],
364
+ value="Neural Network",
365
+ label="Select Model"
366
+ )
367
+
368
+ student_time = gr.Slider(
369
+ 0, 20,
370
+ value=10,
371
+ label="Study Time Weekly"
372
+ )
373
+
374
+ absences = gr.Slider(
375
+ 0, 30,
376
+ value=5,
377
+ label="Absences"
378
+ )
379
+
380
+ parental_support = gr.Slider(
381
+ 0, 4,
382
+ value=2,
383
+ step=1,
384
+ label="Parental Support"
385
+ )
386
 
387
  tutoring = gr.Checkbox(label="Tutoring")
388
  extracurricular = gr.Checkbox(label="Extracurricular Activities")
 
390
  music = gr.Checkbox(label="Music")
391
  volunteering = gr.Checkbox(label="Volunteering")
392
 
393
+ btn = gr.Button(
394
+ "Predict",
395
+ variant="primary"
396
+ )
397
 
398
  with gr.Column():
399
+
400
  result = gr.Textbox(label="Prediction")
401
+
402
+ confidence = gr.Number(
403
+ label="Confidence / Accuracy"
404
+ )
405
 
406
  btn.click(
407
+ predict_model,
408
+ inputs=[
409
+ model_choice,
410
+ student_time,
411
+ absences,
412
+ tutoring,
413
+ parental_support,
414
+ extracurricular,
415
+ sports,
416
+ music,
417
+ volunteering
418
+ ],
419
+ outputs=[
420
+ result,
421
+ confidence
422
+ ]
423
  )
424
 
425
+ gr.Markdown(f"""
426
+ ### Model Training Accuracy
427
+ - Decision Tree: {accuracy_tree:.3f}
428
+ - KNN: {accuracy_knn:.3f}
429
+ - Neural Network: {accuracy_nn:.3f}
430
+ """)
431
 
432
  app.launch()