File size: 13,739 Bytes
8fde1ac a7bcef8 |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
#!pip install gradio pandas scikit-learn joblib
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import gradio as gr
import joblib
# Load your dataset (assuming 'AIDA-PHQ-Updated.csv' is in the same directory as your script)
df = pd.read_csv("AIDA-PHQ-Updated.csv")
df.head()
# Ordinal mappings (if not already applied)
ordinal_mappings = {
'Not Applicable' : 0,
'Strongly disagree': 1,
'Disagree': 2,
'Neutral': 3,
'Agree': 4,
'Strongly agree': 5
}
ordinal_columns = [
'PoorAcademicPerformanceSelfPerception', 'AcademicCriticismSelfPerception',
'UnsatisfiedAcademicWorkloadSelfPerception', 'NonInterestSubjectOpinion',
'UnhappySubjectOpinion', 'NonInterestInstitutionOpinion',
'UnhappyInstitutionOpinion', 'ParentalStrictness', 'ParentalAcademicPressure',
'ParentalMarriagePressure', 'ParentalCareerPressure',
'ParentalStudyAbroadPressure', 'ParentalUnderstanding', 'SiblingBonding',
'ParentalRelationshipStability', 'PeerRelationship', 'TeacherSupport',
'PartnerRelationshipImpact', 'PhysicalViolenceExperience',
'SexualViolenceExperience', 'VerbalViolenceExperience',
'EmotionalViolenceExperience'
]
for col in ordinal_columns:
df[col] = df[col].map(ordinal_mappings)
# Encode the Gender column
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
df['Gender'] = encoder.fit_transform(df['Gender'])
df.head()
# Split into X and y
X = df.drop('DepressionLevel', axis=1)
y = df['DepressionLevel']
X.columns
X.head()
# Split into train and test sets
random_state = 2024
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_state)
# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train Logistic Regression model
logreg = LogisticRegression(C=10.0, solver='newton-cg')
logreg.fit(X_train_scaled, y_train)
# Save the model as a .bin file
with open('model.bin', 'wb') as file:
joblib.dump(logreg, file)
# Save the scaler as a .bin file
with open('scaler.bin', 'wb') as file:
joblib.dump(scaler, file)
def predict_depression_level_logreg(age, gender, cgpa, poor_academic_performance,
academic_criticism, unsatisfied_workload,
non_interest_subject, unhappy_subject,
non_interest_institution, unhappy_institution,
parental_strictness, parental_academic_pressure,
parental_marriage_pressure, parental_career_pressure,
parental_study_abroad, parental_understanding,
sibling_bonding, parental_relationship_stability,
peer_relationship, teacher_support,
partner_relationship_impact, physical_violence,
sexual_violence, verbal_violence, emotional_violence,
little_interest, feeling_down, sleeping_issue,
feeling_tired, poor_appetite, feeling_bad,
trouble_concentrating, slowness, self_harm):
# Convert gender to numeric
gender = 1 if gender == 'Female' else 0
# Define feature names matching those used during fitting
feature_names = [
'Age', 'Gender', 'CGPA', 'PoorAcademicPerformanceSelfPerception', 'AcademicCriticismSelfPerception',
'UnsatisfiedAcademicWorkloadSelfPerception', 'NonInterestSubjectOpinion', 'UnhappySubjectOpinion',
'NonInterestInstitutionOpinion', 'UnhappyInstitutionOpinion', 'ParentalStrictness', 'ParentalAcademicPressure',
'ParentalMarriagePressure', 'ParentalCareerPressure', 'ParentalStudyAbroadPressure', 'ParentalUnderstanding',
'SiblingBonding', 'ParentalRelationshipStability', 'PeerRelationship', 'TeacherSupport',
'PartnerRelationshipImpact', 'PhysicalViolenceExperience', 'SexualViolenceExperience', 'VerbalViolenceExperience',
'EmotionalViolenceExperience', 'little interest', 'feeling down', 'Sleeping issue', 'feeling tired',
'poor appetite', 'feeling bad', 'trouble concertrating', 'slowness', 'self harm'
]
# Map ordinal columns to numerical values using ordinal_mappings
input_data = pd.DataFrame([[age, gender, cgpa, ordinal_mappings[poor_academic_performance],
ordinal_mappings[academic_criticism], ordinal_mappings[unsatisfied_workload],
ordinal_mappings[non_interest_subject], ordinal_mappings[unhappy_subject],
ordinal_mappings[non_interest_institution], ordinal_mappings[unhappy_institution],
ordinal_mappings[parental_strictness], ordinal_mappings[parental_academic_pressure],
ordinal_mappings[parental_marriage_pressure], ordinal_mappings[parental_career_pressure],
ordinal_mappings[parental_study_abroad], ordinal_mappings[parental_understanding],
ordinal_mappings[sibling_bonding], ordinal_mappings[parental_relationship_stability],
ordinal_mappings[peer_relationship], ordinal_mappings[teacher_support],
ordinal_mappings[partner_relationship_impact], ordinal_mappings[physical_violence],
ordinal_mappings[sexual_violence], ordinal_mappings[verbal_violence], ordinal_mappings[emotional_violence],
little_interest, feeling_down, sleeping_issue, feeling_tired,
poor_appetite, feeling_bad, trouble_concentrating, slowness, self_harm]],
columns=feature_names)
input_data_scaled = scaler.transform(input_data)
prediction = logreg.predict(input_data_scaled)[0]
return str(prediction)
# Define Gradio interface with proper input labels and output type
inputs = [
gr.Slider(minimum=int(df['Age'].min()), maximum=int(df['Age'].max()), label="Age"),
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
gr.Slider(minimum=float(df['CGPA'].min()), maximum=float(df['CGPA'].max()), label="CGPA")
]
# Updated labels for ordinal columns
ordinal_labels = {
'PoorAcademicPerformanceSelfPerception': 'Your Academic Performance is poor.',
'AcademicCriticismSelfPerception': 'You experience Academic Criticism.',
'UnsatisfiedAcademicWorkloadSelfPerception': 'You are unsatisfied with your academic workload.',
'NonInterestSubjectOpinion': 'The subject you are studying is of non-interest to you.',
'UnhappySubjectOpinion': 'You are unhappy with the subject you are studying.',
'NonInterestInstitutionOpinion': 'You study at an institution of your non-interest.',
'UnhappyInstitutionOpinion': 'You are Unhappy with your institution.',
'ParentalStrictness': 'Your parents are strict.',
'ParentalAcademicPressure': 'You experience academic pressure from your parents.',
'ParentalMarriagePressure': 'You experience pressure to get married from your parents.',
'ParentalCareerPressure': 'You experience career pressure from your parents.',
'ParentalStudyAbroadPressure': 'You experience pressure to study abroad from your parents.',
'ParentalUnderstanding': 'Your have poor understanding with your parents.',
'SiblingBonding': 'You have poor bonding with your siblings.',
'ParentalRelationshipStability': 'Your parents have unstable relationship.',
'PeerRelationship': 'You have poor relationship with your peers.',
'TeacherSupport': 'Teachers do not support you.',
'PartnerRelationshipImpact': 'You have poor relationship with your partner.',
'PhysicalViolenceExperience': 'You have experience physical violence.',
'SexualViolenceExperience': 'You have experience sexual violence.',
'VerbalViolenceExperience': 'You have experience verbal violence.',
'EmotionalViolenceExperience': 'You have experienced emotional violence.',
}
# Add radio buttons for ordinal columns with updated labels
for col in ordinal_columns:
inputs.append(gr.Radio(choices=list(ordinal_mappings.keys()), label=ordinal_labels[col]))
# Add sliders for the remaining inputs
additional_inputs = [
gr.Slider(minimum=0, maximum=5, step=1, label="How has your interest changed over work and activities? (0= No change)"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel down?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Do you struggle to sleep?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel tired?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How has your appetite changed?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel bad about yourself?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How has your concentration levels changed?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Do you feel slow?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Have you had suicidal thoughts?")
]
inputs.extend(additional_inputs)
# # Sample input values (these should match the features and their order in your function)
# sample_input = {
# "Age": 25,
# "Gender": "Female", # Assuming string value for Gender
# "CGPA": 3.5,
# "PoorAcademicPerformanceSelfPerception": "Disagree", # String value, needs mapping
# "AcademicCriticismSelfPerception": "Neutral", # String value, needs mapping
# "UnsatisfiedAcademicWorkloadSelfPerception": "Strongly disagree", # String value, needs mapping
# "NonInterestSubjectOpinion": "Disagree", # String value, needs mapping
# "UnhappySubjectOpinion": "Neutral", # String value, needs mapping
# "NonInterestInstitutionOpinion": "Strongly disagree", # String value, needs mapping
# "UnhappyInstitutionOpinion": "Disagree", # String value, needs mapping
# "ParentalStrictness": "Strongly agree", # String value, needs mapping
# "ParentalAcademicPressure": "Agree", # String value, needs mapping
# "ParentalMarriagePressure": "Neutral", # String value, needs mapping
# "ParentalCareerPressure": "Strongly disagree", # String value, needs mapping
# "ParentalStudyAbroadPressure": "Neutral", # String value, needs mapping
# "ParentalUnderstanding": "Strongly agree", # String value, needs mapping
# "SiblingBonding": "Strongly agree", # String value, needs mapping
# "ParentalRelationshipStability": "Neutral", # String value, needs mapping
# "PeerRelationship": "Agree", # String value, needs mapping
# "TeacherSupport": "Strongly agree", # String value, needs mapping
# "PartnerRelationshipImpact": "Neutral", # String value, needs mapping
# "PhysicalViolenceExperience": 0,
# "SexualViolenceExperience": 1,
# "VerbalViolenceExperience": 0,
# "EmotionalViolenceExperience": 2,
# "little interest": 1,
# "feeling down": 3,
# "Sleeping issue": 2,
# "feeling tired": 4,
# "poor appetite": 2,
# "feeling bad": 3,
# "trouble concertrating": 1,
# "slowness": 2,
# "self harm": 0
# }
# # Convert the sample input dictionary to a DataFrame
# sample_df = pd.DataFrame([sample_input])
# # Map ordinal columns to numerical values using ordinal_mappings
# for col in ordinal_columns:
# sample_df[col] = sample_df[col].map(ordinal_mappings)
# # Convert gender to numeric
# sample_df['Gender'] = 1 if sample_df['Gender'].iloc[0] == 'Female' else 0
# # Scale the input data using the scaler from your model
# sample_df_scaled = scaler.transform(sample_df)
# # Predict using your function
# prediction = predict_depression_level_logreg(*sample_df_scaled[0])
# # Print or use the prediction as needed
# print("Predicted Depression Level:", prediction)
output = gr.Textbox(label="Predicted Depression Level")
# Create Gradio interface
iface = gr.Interface(fn=predict_depression_level_logreg, inputs=inputs, outputs=output, title="Understand your Depression Levels",
description="A questionnaire to determine potential depression severity using the questions below - ")
iface.launch(debug=True, share=True)
# #importing libraries
# import numpy as np
# import lime
# # import lime.lime_tabular
# from lime import lime_tabular
# # Training Logistic Regression classifier
# model = LogisticRegression()
# model.fit(X_train, y_train)
# # Defining the lime explainer
# explainer = lime_tabular.LimeTabularExplainer(
# training_data=np.array(X_train),
# feature_names=X_train.columns,
# class_names=['MINIMAL','MILD','Moderate','ModeratelySevere','Severe'],
# mode='classification'
# )
# #importing libraries
# import numpy as np
# import lime
# # import lime.lime_tabular
# from lime import lime_tabular
# # Training Logistic Regression classifier
# model = LogisticRegression()
# model.fit(X_train, y_train)
# # Defining the lime explainer
# explainer = lime_tabular.LimeTabularExplainer(
# training_data=np.array(X_train),
# feature_names=X_train.columns,
# class_names=['MINIMAL','MILD','Moderate','ModeratelySevere','Severe'],
# mode='classification'
# )
# exp = explainer.explain_instance(
# data_row=X_test.iloc[5],
# predict_fn=model.predict_proba, num_features = 12
# )
# exp.show_in_notebook(show_table=True)
|