-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
438 lines (391 loc) · 14.9 KB
/
app.py
File metadata and controls
438 lines (391 loc) · 14.9 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
# -----------------------------
# Page configuration
# -----------------------------
st.set_page_config(
page_title="Student Performance Analysis",
page_icon="📊",
layout="wide"
)
# -----------------------------
# Title & intro
# -----------------------------
st.title("Student Performance Analysis")
st.markdown("### What Really Affects Exam Scores?")
st.write(
"This project explores which factors have the biggest impact on "
"student exam scores, with a focus on study hours, sleep, and "
"other lifestyle and school-related variables."
)
# -----------------------------
# Load data
# -----------------------------
@st.cache_data
def load_data():
cwd = Path(__file__).parent
possible_paths = [
cwd / "Data" / "Cleaned" / "cleaned_student_data.csv",
cwd / "Data" / "cleaned_student_data.csv",
cwd / "cleaned_student_data.csv",
cwd / "Data" / "Raw" / "StudentPerformanceFactors.csv",
]
for p in possible_paths:
if p.exists():
try:
df = pd.read_csv(p)
return df
except Exception as e:
st.error(f"Failed to read {p}: {e}")
return None
return None
df = load_data()
if df is not None:
# -----------------------------
# Basic cleaning / formatting
# -----------------------------
df_clean = df.copy()
df_clean.columns = [c.strip().replace(" ", "_") for c in df_clean.columns]
categorical_columns = [
"Gender",
"Parental_Involvement",
"Access_to_Resources",
"Extracurricular_Activities",
"Motivation_Level",
"Internet_Access",
"Family_Income",
"Teacher_Quality",
"School_Type",
"Peer_Influence",
"Learning_Disabilities",
"Parental_Education_Level",
"Distance_from_Home",
]
for col in categorical_columns:
if col in df_clean.columns:
df_clean[col] = df_clean[col].astype("category")
# Create Study_Sleep_Group
if (("Hours_Studied" in df_clean.columns)
and ("Sleep_Hours" in df_clean.columns)):
study_threshold = df_clean["Hours_Studied"].median()
sleep_threshold = df_clean["Sleep_Hours"].median()
def create_study_sleep_group(row):
high_study = row["Hours_Studied"] > study_threshold
high_sleep = row["Sleep_Hours"] > sleep_threshold
if high_study and high_sleep:
return "High Both"
elif high_study and not high_sleep:
return "High Study Only"
elif not high_study and high_sleep:
return "High Sleep Only"
else:
return "Low Both"
df_clean["Study_Sleep_Group"] = df_clean.apply(
create_study_sleep_group, axis=1)
# -----------------------------
# Sidebar
# -----------------------------
st.sidebar.header("Filter & Navigation")
page = st.sidebar.radio(
"Go to",
[
"📋 Overview",
"🔑 Key Findings",
"📈 Visualisations",
"📊 Data Explorer",
"🔮 Score Predictor",
"📝 About",
],
)
# Optional global filter (example: gender)
if "Gender" in df_clean.columns:
st.sidebar.subheader("Quick Filter")
gender_options = (
df_clean["Gender"].cat.categories.tolist()
if hasattr(df_clean["Gender"], "cat")
else df_clean["Gender"].unique()
)
selected_gender = st.sidebar.multiselect(
"Filter by Gender",
options=gender_options,
default=gender_options,
)
df_view = df_clean[df_clean["Gender"].isin(selected_gender)]
else:
df_view = df_clean
# -----------------------------
# Overview
# -----------------------------
if page == "Overview":
st.header("Project Overview")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Students", len(df_view))
with col2:
st.metric("Features", len(df_view.columns))
with col3:
if "Exam_Score" in df_view.columns:
avg_score = df_view["Exam_Score"].mean()
st.metric("Avg Exam Score", f"{avg_score:.2f}")
else:
st.metric("Avg Exam Score", "N/A")
st.subheader("Data Sample")
st.dataframe(df_view.head(10), use_container_width=True)
st.subheader("High-Level Dataset Summary")
st.write(
f"- **Rows:** {df_view.shape[0]} \n"
f"- **Columns:** {df_view.shape[1]} \n"
"- Mix of numerical and categorical features related to "
"study habits, sleep, school environment, and family background."
)
st.subheader("Business Requirements")
st.markdown(
"- **BR1:** Identify the key drivers of student performance.\n"
"- **BR2:** Understand how study and sleep "
"balance relate to exam scores.\n"
"- **BR3:** Provide a simple way to estimate a student’s "
"potential score."
)
st.subheader("Research Hypotheses")
st.markdown(
"- **H1:** Hours studied explains more variance in exam "
"scores than attendance or sleep.\n"
"- **H2:** Students with both high study hours and high sleep "
"perform best.\n"
)
# -----------------------------
# Key Findings
# -----------------------------
elif page == "🔑 Key Findings":
st.header("Key Findings")
with st.expander(
"Finding 1: Study Hours Are the Strongest Driver",
expanded=True
):
st.write(
"**What we found:** Study hours show the strongest "
"relationship with exam score compared to other single "
"factors like attendance or sleep."
)
fig, ax = plt.subplots(figsize=(8, 4))
predictors = ["Hours Studied", "Attendance", "Sleep Hours"]
# Example R²-style values
# (you can adjust these to match your notebook)
r2_values = [0.20, 0.05, 0.03]
colors = ["#2E86AB", "#A23B72", "#A23B72"]
ax.bar(predictors, r2_values, color=colors)
ax.set_ylabel("Relative Importance (R²-like)")
ax.set_ylim(0, 0.25)
st.pyplot(fig)
if ("Study_Sleep_Group" in df_view.columns
and "Exam_Score" in df_view.columns):
with st.expander(
"Finding 2: Study + Sleep Balance Matters",
expanded=True
):
st.write(
"Students who manage **both** higher study hours and "
"higher sleep tend to achieve the best scores overall."
)
fig, ax = plt.subplots(figsize=(8, 4))
sns.boxplot(
data=df_view,
x="Study_Sleep_Group",
y="Exam_Score",
ax=ax,
)
ax.set_xlabel("Study–Sleep Group")
ax.set_ylabel("Exam Score")
st.pyplot(fig)
# -----------------------------
# Visualisations
# -----------------------------
elif page == "📈 Visualisations":
st.header("Visualisations")
tab1, tab2 = st.tabs(["Correlation Matrix", "Study–Sleep Groups"])
with tab1:
st.subheader("Correlation Matrix (Numerical Features)")
numerical_df = df_view.select_dtypes(include=[np.number])
if len(numerical_df.columns) > 1:
fig, ax = plt.subplots(figsize=(10, 6))
sns.heatmap(
numerical_df.corr(),
annot=True,
cmap="coolwarm",
ax=ax,
)
st.pyplot(fig)
else:
st.info(
"Not enough numerical columns to show a "
"correlation matrix."
)
with tab2:
st.subheader("Exam Score by Study–Sleep Group")
if ("Study_Sleep_Group" in df_view.columns
and "Exam_Score" in df_view.columns):
fig, ax = plt.subplots(figsize=(10, 6))
sns.boxplot(
data=df_view,
x="Study_Sleep_Group",
y="Exam_Score",
ax=ax,
)
ax.set_xlabel("Study–Sleep Group")
ax.set_ylabel("Exam Score")
st.pyplot(fig)
else:
st.info(
"Study_Sleep_Group or Exam_Score not available in "
"the dataset."
)
# -----------------------------
# Data Explorer
# -----------------------------
elif page == "📊 Data Explorer":
st.header("Explore the Data")
# Extra filters
df_explore = df_view.copy()
if "School_Type" in df_explore.columns:
school_options = df_explore["School_Type"].unique()
selected_school = st.multiselect(
"Filter by School Type",
options=school_options,
default=school_options,
)
df_explore = df_explore[
df_explore["School_Type"].isin(selected_school)
]
if "Parental_Involvement" in df_explore.columns:
involvement_options = (
df_explore["Parental_Involvement"].unique()
)
selected_involvement = st.multiselect(
"Filter by Parental Involvement",
options=involvement_options,
default=involvement_options,
)
df_explore = df_explore[
df_explore["Parental_Involvement"].isin(
selected_involvement
)
]
st.subheader("Filtered Data")
st.dataframe(df_explore, use_container_width=True)
# -----------------------------
# Score Predictor
# -----------------------------
elif page == "🔮 Score Predictor":
st.header("Simple Score Predictor")
st.write(
"This is a **simple, data-inspired tool** that "
" estimates a student's exam score "
"based mainly on their study hours (and optionally sleep). "
"It’s not a full machine learning model, but it follows the "
" patterns seen in the data."
)
if ("Hours_Studied" in df_clean.columns
and "Exam_Score" in df_clean.columns):
# Fit a simple linear relationship: Exam_Score ~ Hours_Studied
x = df_clean["Hours_Studied"].values
y = df_clean["Exam_Score"].values
# Remove any NaNs
mask = ~np.isnan(x) & ~np.isnan(y)
x = x[mask]
y = y[mask]
if len(x) > 1:
# Simple linear regression using numpy
slope, intercept = np.polyfit(x, y, 1)
st.subheader("Enter Your Details")
col1, col2 = st.columns(2)
with col1:
user_hours = st.number_input(
"Study Hours per Week",
min_value=0.0,
max_value=100.0,
value=float(np.median(x)),
step=1.0,
)
with col2:
if "Sleep_Hours" in df_clean.columns:
sleep_vals = df_clean["Sleep_Hours"].dropna().values
default_sleep = (
float(np.median(sleep_vals))
if len(sleep_vals) > 0
else 7.0
)
user_sleep = st.number_input(
"Sleep Hours per Night",
min_value=0.0,
max_value=16.0,
value=default_sleep,
step=0.5,
)
else:
user_sleep = None
st.info(
"Sleep_Hours not found in dataset, "
"prediction will use study hours only."
)
if st.button("Estimate Score"):
base_pred = intercept + slope * user_hours
# Optional gentle adjustment based on sleep
if user_sleep is not None:
# Assume 7–9 hours is ideal range
if 7 <= user_sleep <= 9:
base_pred += 3 # small bonus
elif user_sleep < 5:
base_pred -= 3 # small penalty
predicted_score = float(np.clip(base_pred, 0, 100))
score_text = (
f"Estimated Exam Score: "
f"**{predicted_score:.1f} / 100**"
)
st.success(score_text)
st.caption(
"This is a rough estimate based on patterns in "
"the dataset, not a guaranteed result."
)
st.subheader("How the Model Sees Study Hours")
fig, ax = plt.subplots(figsize=(8, 4))
ax.scatter(x, y, alpha=0.4, label="Students")
x_line = np.linspace(x.min(), x.max(), 100)
y_line = intercept + slope * x_line
ax.plot(x_line, y_line, color="red", label="Trend Line")
ax.set_xlabel("Hours Studied per Week")
ax.set_ylabel("Exam Score")
ax.legend()
st.pyplot(fig)
else:
st.info("Not enough data to build a predictor.")
else:
st.info(
"Required columns (Hours_Studied and Exam_Score) "
"are not available in the dataset."
)
# -----------------------------
# About
# -----------------------------
elif page == "📝 About":
st.header("About This Project")
st.write("**Author:** Sadiyah")
st.write("**Dataset:** Student Performance Factors (Kaggle)")
st.write(
"This dashboard was built to explore what really affects "
"exam scores, with a focus on making insights clear, "
"visual, and interactive."
)
st.markdown("---")
st.caption(
"Created as part of a data analytics project on "
"student performance factors."
)
else:
st.error(
"Data file not found! "
"Please check your file paths in the repository."
)