File size: 1,437 Bytes
d6db648 |
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 |
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import io
def predict_engagement(file):
"""
Predicts engagement levels from uploaded student logs CSV.
Assumes a binary 'Engaged' column: 1 = Engaged, 0 = Not Engaged.
Parameters:
file (file-like): CSV file uploaded by user
Returns:
str: Prediction summary and performance metrics (if labeled)
"""
try:
df = pd.read_csv(file)
if 'Engaged' not in df.columns:
return "β CSV must include a binary column named 'Engaged' (1 or 0)."
# Separate features and labels
X = df.drop(columns=['Engaged'])
y = df['Engaged']
# Clean non-numeric columns
X = X.select_dtypes(include=[np.number])
# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Predict
predictions = model.predict(X_test)
report = classification_report(y_test, predictions, target_names=["Not Engaged", "Engaged"])
return "π Engagement Prediction Report:\n\n" + report
except Exception as e:
return f"Error during prediction: {str(e)}"
|