Spaces:
Sleeping
Sleeping
File size: 8,436 Bytes
1869502 8c1223a 1869502 8c1223a 1869502 2f15f91 207db65 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 2f15f91 939dff8 cd94b7b 939dff8 cd94b7b 61e8d1f cd94b7b 2f15f91 cd94b7b 2f15f91 cd94b7b 939dff8 2f15f91 cd94b7b 1ffd3c5 de0d430 cd94b7b 207db65 509debf de0d430 cd94b7b de0d430 cd94b7b 939dff8 de0d430 939dff8 509debf 207db65 1ffd3c5 de0d430 1ffd3c5 939dff8 1ffd3c5 2d4f8a2 939dff8 da83978 8ab78e3 da83978 4157ea9 da83978 cd94b7b 2d4f8a2 192c327 2d4f8a2 8e10056 192c327 2d4f8a2 509debf 61e8d1f da83978 192c327 da83978 bc41b18 ce5c516 cd94b7b 192c327 cd94b7b 2d4f8a2 509debf 50fd136 509debf 61e8d1f da83978 cd94b7b bc41b18 1ffd3c5 cd94b7b de0d430 cd94b7b da83978 bc41b18 08a6438 | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | import numpy as np
from datascience import *
import gradio as gr
SPD = Table.read_table('Student_Performance_Data.csv')
def study_effectiveness(study_time, absences, parental_support, tutoring):
return (study_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
effectiveness_scores = SPD.apply(
study_effectiveness,
'StudyTimeWeekly',
'Absences',
'ParentalSupport',
'Tutoring'
)
SPD = SPD.with_column('StudyEffectiveness', effectiveness_scores)
def grade_to_gpa(grade):
if grade == 0:
return 4.0
elif grade == 1:
return 3.0
elif grade == 2:
return 2.0
elif grade == 3:
return 1.0
else:
return 0.0
gpas = SPD.apply(grade_to_gpa, 'GradeClass')
SPD = SPD.with_column('GPA', gpas)
effectiveness_array = np.array(SPD.column('StudyEffectiveness'))
gpa_array = np.array(SPD.column('GPA'))
slope = (
np.sum(
(effectiveness_array - np.mean(effectiveness_array)) *
(gpa_array - np.mean(gpa_array))
)
/
np.sum(
(effectiveness_array - np.mean(effectiveness_array)) ** 2
)
)
intercept = np.mean(gpa_array) - (slope * np.mean(effectiveness_array))
def linear_regression_gpa(effectiveness):
gpa = intercept + (slope * effectiveness)
if gpa < 0:
gpa = 0
if gpa > 4:
gpa = 4
return round(gpa, 2)
def predict_tree_gpa(
study_time,
absences,
parental_support,
tutoring,
extracurricular,
sports,
music,
volunteering,
effectiveness
):
if effectiveness >= 18:
gpa = 3.6 + (study_time * 0.02) - (absences * 0.01)
elif effectiveness >= 14:
if absences <= 18:
gpa = 2.8 + (study_time * 0.03) - (absences * 0.015)
else:
gpa = 1.7 - (absences * 0.02)
elif effectiveness >= 10:
if study_time >= 10 and absences <= 12:
gpa = 2.4 + (study_time * 0.03)
elif tutoring == 1 and parental_support >= 3:
gpa = 2.2 + (parental_support * 0.08)
else:
gpa = 1.5 - (absences * 0.02)
elif effectiveness >= 6:
if study_time >= 12 and absences < 8:
gpa = 2.1 + (study_time * 0.025)
elif extracurricular == 1 or sports == 1 or music == 1 or volunteering == 1:
if absences < 12 and parental_support >= 2:
gpa = 2.0 + (parental_support * 0.05)
else:
gpa = 1.3
else:
gpa = 1.0
else:
gpa = 0.6
if gpa < 0:
gpa = 0
if gpa > 4:
gpa = 4
return round(gpa, 2)
data = SPD.select(
'Age',
'Gender',
'Ethnicity',
'ParentalEducation',
'StudyTimeWeekly',
'Absences',
'Tutoring',
'ParentalSupport',
'Extracurricular',
'Sports',
'Music',
'Volunteering',
'StudyEffectiveness',
'GPA'
)
np.random.seed(1)
shuffled = data.sample(with_replacement=False)
size = int(data.num_rows * 0.8)
train = shuffled.take(np.arange(size))
def distance(r1, r2):
total = 0
total += (r1.item('Age') - r2.item('Age'))**2
total += (r1.item('Gender') - r2.item('Gender'))**2
total += (r1.item('Ethnicity') - r2.item('Ethnicity'))**2
total += (r1.item('ParentalEducation') - r2.item('ParentalEducation'))**2
total += (r1.item('StudyTimeWeekly') - r2.item('StudyTimeWeekly'))**2
total += (r1.item('Absences') - r2.item('Absences'))**2
total += (r1.item('Tutoring') - r2.item('Tutoring'))**2
total += (r1.item('ParentalSupport') - r2.item('ParentalSupport'))**2
total += (r1.item('Extracurricular') - r2.item('Extracurricular'))**2
total += (r1.item('Sports') - r2.item('Sports'))**2
total += (r1.item('Music') - r2.item('Music'))**2
total += (r1.item('Volunteering') - r2.item('Volunteering'))**2
total += (r1.item('StudyEffectiveness') - r2.item('StudyEffectiveness'))**2
return np.sqrt(total)
def knn_neighbors(test_row, k):
dists = make_array()
for i in np.arange(train.num_rows):
row = train.row(i)
d = distance(test_row, row)
dists = np.append(dists, d)
temp = train.with_column('Distance', dists)
nearest = temp.sort('Distance').take(np.arange(k))
return nearest
def knn_gpa(test_row, k):
nearest = knn_neighbors(test_row, k)
gpa = np.mean(nearest.column('GPA'))
if gpa < 0:
gpa = 0
if gpa > 4:
gpa = 4
return round(gpa, 2)
k = 5
def predict_models(
outside_study_time,
in_class_learning_time,
attentiveness,
absences,
tutoring,
parental_support,
extracurricular,
sports,
music,
volunteering
):
attention_multiplier = attentiveness / 10
study_time = outside_study_time + (in_class_learning_time * attention_multiplier)
study_effect = (
(study_time * 1.5)
+ (parental_support * 2)
+ (tutoring * 3)
- (absences * 0.7)
)
tree_gpa = predict_tree_gpa(
study_time,
absences,
parental_support,
tutoring,
extracurricular,
sports,
music,
volunteering,
study_effect
)
linear_gpa = linear_regression_gpa(study_effect)
test_row = Table().with_columns(
'Age', [17],
'Gender', [0],
'Ethnicity', [0],
'ParentalEducation', [2],
'StudyTimeWeekly', [study_time],
'Absences', [absences],
'Tutoring', [int(tutoring)],
'ParentalSupport', [parental_support],
'Extracurricular', [int(extracurricular)],
'Sports', [int(sports)],
'Music', [int(music)],
'Volunteering', [int(volunteering)],
'StudyEffectiveness', [study_effect],
'GPA', [0]
).row(0)
knn_prediction = knn_gpa(test_row, k)
final_score = (tree_gpa + linear_gpa + knn_prediction) / 3
tree_label = "PASS" if tree_gpa >= 2.0 else "FAIL"
linear_label = "PASS" if linear_gpa >= 2.0 else "FAIL"
knn_label = "PASS" if knn_prediction >= 2.0 else "FAIL"
final_label = "PASS" if final_score >= 2.0 else "FAIL"
final_output = f"""
<div style="text-align:center; font-size:34px; font-weight:800; margin-top:10px;">
{final_label}
</div>
<div style="text-align:center; font-size:18px; margin-top:10px;">
</div>
"""
return tree_label, linear_label, knn_label, final_output
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate",
radius_size="lg",
text_size="lg"
)
with gr.Blocks(theme=theme, fill_height=True) as app:
gr.Markdown("""
# Student Performance Predictor
### Individual model results + final pass/fail prediction
""")
with gr.Row(equal_height=True):
with gr.Column(scale=1):
with gr.Group():
outside_study_time = gr.Slider(0, 20, value=8, label="Study Time Outside Class Weekly")
in_class_learning_time = gr.Slider(0, 25, value=15, label="Learning Time In Class Weekly")
attentiveness = gr.Slider(1, 10, value=5, step=1, label="Attentiveness In Class")
absences = gr.Slider(0, 30, value=5, label="Absences")
parental_support = gr.Slider(0, 4, value=2, step=1, label="Parental Support")
tutoring = gr.Checkbox(label="Tutoring")
extracurricular = gr.Checkbox(label="Extracurricular Activities")
sports = gr.Checkbox(label="Sports")
music = gr.Checkbox(label="Music")
volunteering = gr.Checkbox(label="Volunteering")
btn = gr.Button("Predict Performance", variant="primary", size="lg")
with gr.Column(scale=1):
tree_output = gr.Textbox(label="Decision Tree", interactive=False)
linear_output = gr.Textbox(label="Linear Regression", interactive=False)
knn_output = gr.Textbox(label="KNN", interactive=False)
final_output = gr.HTML(label="Final Result")
btn.click(
predict_models,
inputs=[
outside_study_time,
in_class_learning_time,
attentiveness,
absences,
tutoring,
parental_support,
extracurricular,
sports,
music,
volunteering
],
outputs=[
tree_output,
linear_output,
knn_output,
final_output
]
)
app.launch() |