rabia31/Student_Performance
0
1import streamlit as st2import pandas as pd3import joblib4import numpy as np5import plotly.express as px6 7# Load data8@st.cache_data9def load_data():10 return pd.read_csv("student_performance.csv")11 12df = load_data()13 14# Load model15@st.cache_resource16def load_model():17 bundle = joblib.load("best_model.pkl")18 model = bundle["model"]19 encoders = bundle["encoders"]20 features = bundle["feature_names"]21 return model, encoders, features22 23model, label_encoders, feature_names = load_model()24 25# Sidebar26st.sidebar.title("๐งญ Navigation")27page = st.sidebar.radio("Go to", ["๐ Introduction", "๐ EDA", "๐ฎ Predict"])28 29# Introduction30if page == "๐ Introduction":31 st.title("๐ Student Final Grade Prediction")32 st.markdown("""33 This app predicts the **final grade (G3)** of students based on their academic performance and personal attributes.34 35 **Dataset:** Student Performance dataset (UCI)36 37 - ๐ฏ Target: `G3` (final grade)38 - ๐ Features: Various student academic and social factors39 """)40 st.metric("๐ Total Records", df.shape[0])41 st.metric("๐งพ Total Features", df.shape[1])42 43 st.subheader("๐ Sample Data")44 st.dataframe(df.head())45 46# EDA47elif page == "๐ EDA":48 st.title("๐ Exploratory Data Analysis")49 50 st.subheader("1๏ธโฃ Summary Statistics")51 st.write(df.describe())52 53 st.subheader("2๏ธโฃ G3 Distribution")54 fig = px.histogram(df, x="G3", nbins=20, title="Distribution of Final Grades (G3)")55 st.plotly_chart(fig)56 57 st.subheader("3๏ธโฃ Correlation with G3")58 corr = df.corr(numeric_only=True)59 corr_g3 = corr["G3"].sort_values(ascending=False)60 st.write(corr_g3)61 fig = px.bar(x=corr_g3.index, y=corr_g3.values, title="Correlation of Features with G3")62 st.plotly_chart(fig)63 64 st.subheader("4๏ธโฃ Study Time vs Final Grade")65 fig = px.box(df, x="studytime", y="G3", title="Study Time vs Final Grade")66 st.plotly_chart(fig)67 68 st.subheader("5๏ธโฃ Past Failures vs Final Grade")69 fig = px.box(df, x="failures", y="G3", title="Past Failures vs Final Grade")70 st.plotly_chart(fig)71 72 st.subheader("6๏ธโฃ Absences vs Final Grade")73 fig = px.scatter(df, x="absences", y="G3", title="Absences vs Final Grade")74 st.plotly_chart(fig)75 76 st.subheader("7๏ธโฃ Going Out vs Final Grade")77 fig = px.box(df, x="goout", y="G3", title="Going Out Frequency vs Final Grade")78 st.plotly_chart(fig)79 80 st.subheader("8๏ธโฃ Gender Distribution")81 fig = px.histogram(df, x="sex", title="Gender Distribution")82 st.plotly_chart(fig)83 84 st.subheader("9๏ธโฃ Address Type Distribution (Urban/Rural)")85 fig = px.histogram(df, x="address", title="Address Type Distribution")86 st.plotly_chart(fig)87 88 st.subheader("๐ Family Size vs Final Grade")89 fig = px.box(df, x="famsize", y="G3", title="Family Size vs Final Grade")90 st.plotly_chart(fig)91 92# Predict93elif page == "๐ฎ Predict":94 st.title("๐ฎ Predict Final Grade (G3)")95 96 col1, col2 = st.columns(2)97 with col1:98 studytime = st.slider("๐ Study Time", 1, 4, 2)99 failures = st.slider("โ Past Failures", 0, 4, 0)100 absences = st.number_input("๐ซ Absences", 0, 100, 4)101 goout = st.slider("๐ Going Out (1-5)", 1, 5, 3)102 103 with col2:104 sex = st.selectbox("๐ป Sex", ["M", "F"])105 address = st.selectbox("๐ Address", ["U", "R"])106 famsize = st.selectbox("๐จโ๐ฉโ๐งโ๐ฆ Family Size", ["LE3", "GT3"])107 Pstatus = st.selectbox("๐จโ๐ฉ Parental Cohabitation", ["T", "A"])108 109 if st.button("๐ฏ Predict G3"):110 # 1๏ธโฃ Create a template DataFrame using dataset median/mode values111 template = df[feature_names].median(numeric_only=True).to_dict()112 113 # Fill categorical features with mode114 for col in feature_names:115 if col in df.columns and not pd.api.types.is_numeric_dtype(df[col]):116 template[col] = df[col].mode()[0]117 118 # 2๏ธโฃ Update with user input119 template.update({120 "studytime": studytime,121 "failures": failures,122 "absences": absences,123 "goout": goout,124 "sex": sex,125 "address": address,126 "famsize": famsize,127 "Pstatus": Pstatus128 })129 130 # 3๏ธโฃ Create DataFrame131 input_df = pd.DataFrame([template])132 133 # 4๏ธโฃ Show filled input values before encoding/prediction134 st.subheader("๐ Filled Input Data Sent to Model")135 st.dataframe(input_df)136 137 # 5๏ธโฃ Encode categorical features138 for col in input_df.columns:139 if col in label_encoders:140 input_df[col] = label_encoders[col].transform(input_df[col].astype(str))141 142 # 6๏ธโฃ Predict143 input_df = input_df[feature_names]144 g3_pred = model.predict(input_df)[0]145 st.success(f"๐ **Predicted Final Grade (G3)**: {g3_pred:.2f}")146 147 