CoolFace
Apppublic

rabia31/Student_Performance

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py147 linesDownload Raw Back to root
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