File size: 3,067 Bytes
3bbe6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import streamlit as st
import altair as alt

# Streamlit app title
st.title("๐Ÿ“Š Student Performance Predictor")

# Load dataset
try:
    df = pd.read_csv("student_performance_data.csv")  # Ensure the file is in the same directory
    st.write("### Preview of Dataset")
    st.write(df.head())  # Show first few rows
except FileNotFoundError:
    st.error("File 'student_performance_data.csv' not found! Please upload the dataset.")

# Ensure dataset contains the required columns
required_columns = ['Study Hours', 'Attendance Rate', 'Assignment Grades', 'Final Exam Score']
if not all(col in df.columns for col in required_columns):
    st.error("Dataset must contain the following columns: " + ", ".join(required_columns))
else:
    # Prepare data for training
    X = df[['Study Hours', 'Attendance Rate', 'Assignment Grades']]
    y = df['Final Exam Score']

    # Split data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    # Train a Linear Regression model
    model = LinearRegression()
    model.fit(X_train, y_train)

    # Make predictions
    y_pred = model.predict(X_test)

    # Evaluate the model
    mse = mean_squared_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)

    # Create tabs
    tab1, tab2, tab3 = st.tabs(["๐Ÿ“ˆ Data Visualization", "๐Ÿ“Š Model Performance", "๐ŸŽฏ Prediction"])

    # Tab 1: Data Visualization
    with tab1:
        st.write("### Data Visualization")
        
        # Scatter plots
        for col in ['Study Hours', 'Attendance Rate', 'Assignment Grades']:
            st.write(f"**{col} vs Final Exam Score**")
            chart = alt.Chart(df).mark_circle().encode(
                x=col,
                y='Final Exam Score',
                tooltip=[col, 'Final Exam Score']
            ).interactive()
            st.altair_chart(chart, use_container_width=True)

    # Tab 2: Model Performance
    with tab2:
        st.write("### Model Performance")
        st.write(f"โœ… Mean Squared Error (MSE): {mse:.2f}")
        st.write(f"โœ… R-squared Score: {r2:.2f}")

    # Tab 3: Prediction
    with tab3:
        st.write("### Predict Final Exam Score")
        study_hours = st.number_input("๐Ÿ“š Study Hours", min_value=0, value=10, step=1)
        attendance_rate = st.slider("๐ŸŽŸ๏ธ Attendance Rate", min_value=0.0, max_value=1.0, step=0.01, value=0.85)
        assignment_grades = st.number_input("๐Ÿ“ Average Assignment Grade", min_value=0, max_value=100, value=80, step=1)

        if st.button("๐Ÿ”ฎ Predict"):
            # Prepare input for prediction
            input_data = np.array([[study_hours, attendance_rate, assignment_grades]])

            # Make prediction
            predicted_score = model.predict(input_data)[0]

            st.success(f"๐ŸŽฏ Predicted Final Exam Score: {predicted_score:.2f}")