Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from sklearn.preprocessing import LabelEncoder
|
|
| 6 |
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
from sklearn.metrics import accuracy_score, classification_report
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def load_data():
|
| 11 |
df = pd.read_csv(r"C:\Users\pavilion\Desktop\INNOMATICS_PROJECTS(SL)\Student_Depresion_Prediction(Machine_Learning)\student_depression_dataset.csv")
|
| 12 |
|
|
@@ -20,67 +20,52 @@ def load_data():
|
|
| 20 |
|
| 21 |
return df
|
| 22 |
|
| 23 |
-
#
|
| 24 |
def preprocess_data(df):
|
| 25 |
label_encoders = {}
|
| 26 |
-
|
| 27 |
-
# Identify categorical columns
|
| 28 |
categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
|
| 29 |
|
| 30 |
for col in categorical_cols:
|
| 31 |
le = LabelEncoder()
|
| 32 |
-
df[col] = le.fit_transform(df[col])
|
| 33 |
-
label_encoders[col] = le
|
| 34 |
|
| 35 |
return df, label_encoders
|
| 36 |
|
| 37 |
-
#
|
| 38 |
def train_model(X_train, y_train):
|
| 39 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 40 |
-
model.fit(X_train, y_train)
|
| 41 |
return model
|
| 42 |
|
| 43 |
-
#
|
| 44 |
def main():
|
| 45 |
st.set_page_config(page_title="Student Depression Prediction", layout="wide")
|
| 46 |
|
| 47 |
-
st.markdown("<h1 style='text-align: center;'>π Student Depression Prediction
|
| 48 |
st.write("This app predicts whether a student is likely to experience depression based on various factors.")
|
| 49 |
|
| 50 |
-
# Load dataset
|
| 51 |
df = load_data()
|
| 52 |
-
|
| 53 |
-
# Preprocess data
|
| 54 |
df, label_encoders = preprocess_data(df)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
y = df['Depression'] # Target (Depression)
|
| 59 |
|
| 60 |
-
# Train/Test split
|
| 61 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
|
| 62 |
-
|
| 63 |
-
# Train model
|
| 64 |
model = train_model(X_train, y_train)
|
| 65 |
y_pred = model.predict(X_test)
|
| 66 |
|
| 67 |
-
# Model evaluation
|
| 68 |
accuracy = accuracy_score(y_test, y_pred)
|
| 69 |
classification_rep = classification_report(y_test, y_pred)
|
| 70 |
|
| 71 |
-
# Train Model Button
|
| 72 |
if st.button("π Train Model"):
|
| 73 |
st.subheader("β
Model Trained Successfully!")
|
| 74 |
st.write(f"**Accuracy:** {accuracy:.2f}")
|
| 75 |
st.text("Classification Report:")
|
| 76 |
st.text(classification_rep)
|
| 77 |
|
| 78 |
-
# Feature Importance
|
| 79 |
feature_importances = model.feature_importances_
|
| 80 |
-
feature_importance_df = pd.DataFrame({
|
| 81 |
-
'Feature': X.columns,
|
| 82 |
-
'Importance': feature_importances
|
| 83 |
-
}).sort_values(by='Importance', ascending=False)
|
| 84 |
|
| 85 |
st.subheader("π Feature Importance")
|
| 86 |
fig, ax = plt.subplots()
|
|
@@ -92,14 +77,13 @@ def main():
|
|
| 92 |
|
| 93 |
# User Input for Prediction
|
| 94 |
st.subheader("π Predict Depression Risk")
|
| 95 |
-
|
| 96 |
input_data = {}
|
| 97 |
|
| 98 |
for col in X.columns:
|
| 99 |
-
if col in label_encoders:
|
| 100 |
input_data[col] = st.selectbox(f"Select {col}", label_encoders[col].classes_)
|
| 101 |
input_data[col] = label_encoders[col].transform([input_data[col]])[0]
|
| 102 |
-
else:
|
| 103 |
input_data[col] = st.number_input(f"Enter {col}", min_value=int(X[col].min()), max_value=int(X[col].max()), value=int(X[col].mean()))
|
| 104 |
|
| 105 |
# Prediction Button
|
|
@@ -108,9 +92,25 @@ def main():
|
|
| 108 |
prediction = model.predict(input_df)[0]
|
| 109 |
|
| 110 |
if prediction == 1:
|
| 111 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Run the Streamlit app
|
| 116 |
if __name__ == '__main__':
|
|
|
|
| 6 |
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
from sklearn.metrics import accuracy_score, classification_report
|
| 8 |
|
| 9 |
+
# Load dataset
|
| 10 |
def load_data():
|
| 11 |
df = pd.read_csv(r"C:\Users\pavilion\Desktop\INNOMATICS_PROJECTS(SL)\Student_Depresion_Prediction(Machine_Learning)\student_depression_dataset.csv")
|
| 12 |
|
|
|
|
| 20 |
|
| 21 |
return df
|
| 22 |
|
| 23 |
+
# Preprocess data
|
| 24 |
def preprocess_data(df):
|
| 25 |
label_encoders = {}
|
|
|
|
|
|
|
| 26 |
categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
|
| 27 |
|
| 28 |
for col in categorical_cols:
|
| 29 |
le = LabelEncoder()
|
| 30 |
+
df[col] = le.fit_transform(df[col])
|
| 31 |
+
label_encoders[col] = le
|
| 32 |
|
| 33 |
return df, label_encoders
|
| 34 |
|
| 35 |
+
# Train model
|
| 36 |
def train_model(X_train, y_train):
|
| 37 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 38 |
+
model.fit(X_train, y_train)
|
| 39 |
return model
|
| 40 |
|
| 41 |
+
# Streamlit App
|
| 42 |
def main():
|
| 43 |
st.set_page_config(page_title="Student Depression Prediction", layout="wide")
|
| 44 |
|
| 45 |
+
st.markdown("<h1 style='text-align: center;'>π Student Depression Prediction</h1>", unsafe_allow_html=True)
|
| 46 |
st.write("This app predicts whether a student is likely to experience depression based on various factors.")
|
| 47 |
|
|
|
|
| 48 |
df = load_data()
|
|
|
|
|
|
|
| 49 |
df, label_encoders = preprocess_data(df)
|
| 50 |
|
| 51 |
+
X = df.drop(columns=['Depression'])
|
| 52 |
+
y = df['Depression']
|
|
|
|
| 53 |
|
|
|
|
| 54 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
|
|
|
|
|
|
|
| 55 |
model = train_model(X_train, y_train)
|
| 56 |
y_pred = model.predict(X_test)
|
| 57 |
|
|
|
|
| 58 |
accuracy = accuracy_score(y_test, y_pred)
|
| 59 |
classification_rep = classification_report(y_test, y_pred)
|
| 60 |
|
|
|
|
| 61 |
if st.button("π Train Model"):
|
| 62 |
st.subheader("β
Model Trained Successfully!")
|
| 63 |
st.write(f"**Accuracy:** {accuracy:.2f}")
|
| 64 |
st.text("Classification Report:")
|
| 65 |
st.text(classification_rep)
|
| 66 |
|
|
|
|
| 67 |
feature_importances = model.feature_importances_
|
| 68 |
+
feature_importance_df = pd.DataFrame({'Feature': X.columns, 'Importance': feature_importances}).sort_values(by='Importance', ascending=False)
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
st.subheader("π Feature Importance")
|
| 71 |
fig, ax = plt.subplots()
|
|
|
|
| 77 |
|
| 78 |
# User Input for Prediction
|
| 79 |
st.subheader("π Predict Depression Risk")
|
|
|
|
| 80 |
input_data = {}
|
| 81 |
|
| 82 |
for col in X.columns:
|
| 83 |
+
if col in label_encoders:
|
| 84 |
input_data[col] = st.selectbox(f"Select {col}", label_encoders[col].classes_)
|
| 85 |
input_data[col] = label_encoders[col].transform([input_data[col]])[0]
|
| 86 |
+
else:
|
| 87 |
input_data[col] = st.number_input(f"Enter {col}", min_value=int(X[col].min()), max_value=int(X[col].max()), value=int(X[col].mean()))
|
| 88 |
|
| 89 |
# Prediction Button
|
|
|
|
| 92 |
prediction = model.predict(input_df)[0]
|
| 93 |
|
| 94 |
if prediction == 1:
|
| 95 |
+
st.error("π¨ **The student is at risk of depression.**")
|
| 96 |
+
st.subheader("π Recommended Actions:")
|
| 97 |
+
st.markdown("""
|
| 98 |
+
1. **Seek Professional Help** π©ββοΈ - Consult a therapist or counselor.
|
| 99 |
+
2. **Stay Connected with Loved Ones** π‘ - Talk to trusted friends or family.
|
| 100 |
+
3. **Engage in Physical Activities** πββοΈ - Exercise regularly to improve mood.
|
| 101 |
+
4. **Practice Mindfulness & Relaxation** π§ββοΈ - Try meditation, yoga, or deep breathing.
|
| 102 |
+
5. **Manage Academic Stress** π - Break tasks into smaller steps and seek support.
|
| 103 |
+
""")
|
| 104 |
else:
|
| 105 |
+
st.success("π₯³ **The student is not at risk of depression.**")
|
| 106 |
+
st.subheader("β
Wellness Tips:")
|
| 107 |
+
st.markdown("""
|
| 108 |
+
1. **Maintain a Balanced Lifestyle** βοΈ - Manage studies, social life, and self-care.
|
| 109 |
+
2. **Get Enough Sleep** π΄ - Ensure 7β9 hours of quality sleep for better mental health.
|
| 110 |
+
3. **Engage in Hobbies & Creative Activities** π¨ - Pursue music, reading, or painting.
|
| 111 |
+
4. **Stay Active & Exercise** π΄ββοΈ - Physical activity boosts energy and reduces anxiety.
|
| 112 |
+
5. **Support Others & Build Strong Friendships** π€ - Encourage open discussions and check on friends.
|
| 113 |
+
""")
|
| 114 |
|
| 115 |
# Run the Streamlit app
|
| 116 |
if __name__ == '__main__':
|