Advanced machine learning approaches for mental health risk assessment using both survey data and social media text analysis
MindScope demonstrates two complementary approaches to AI-powered mental health risk assessment:
- π Survey-Based ML - Traditional machine learning on psychological questionnaire data
- π€ Text Classification - Modern NLP using DistilBERT for social media text analysis
Both approaches achieve excellent performance and showcase different aspects of machine learning engineering.
- 75.3% Accuracy on psychological survey data
- Optimized Random Forest with feature engineering
- 18 psychological features including composite scores
- Comprehensive model comparison (4 algorithms tested)
- 90% Accuracy on mental health text data
- 88.9% F1 Score with balanced precision/recall
- Transformer fine-tuning on authentic social media content
- Attention visualization for model interpretability
- PyTorch - Deep learning framework
- Transformers (HuggingFace) - DistilBERT implementation
- scikit-learn - Traditional ML algorithms
- pandas & numpy - Data manipulation
- Streamlit - Interactive web applications
- Plotly - Dynamic visualizations
- matplotlib & seaborn - Statistical plots
- Hyperparameter tuning with RandomizedSearchCV
- Feature engineering with polynomial features
- Ensemble methods and voting classifiers
mindscope/
βββ π version1_survey/ # Survey-based ML approach
β βββ notebooks/
β β βββ 01_data_exploration.py
β β βββ 02_data_preprocessing.py
β β βββ 03_model_training.py
β β βββ 04_model_optimization.py
β βββ models/
β β βββ saved_models/ # Original models (72.5% accuracy)
β β βββ optimized/ # Optimized models (75.3% accuracy)
β βββ app/
β β βββ streamlit_app.py # Survey assessment interface
β βββ results/
β βββ figures/ # Performance visualizations
β βββ metrics/ # Model evaluation results
β
βββ π€ version2_text/ # DistilBERT text classification
β βββ notebooks/
β β βββ 01_reddit_data_collection.py
β β βββ 02_distilbert_text_classification.py
β βββ models/ # Trained DistilBERT model (90% accuracy)
β βββ app/
β β βββ streamlit_text_app.py # Text analysis interface
β βββ results/
β βββ figures/ # Training visualizations
β
βββ π data/
β βββ survey_data/ # Psychological questionnaire data
β βββ text_data/ # Mental health social media text
β
βββ requirements.txt
βββ README.md
# Clone the repository
git clone https://github.com/Ankit-Anand123/mindscope.git
cd mindscope
# Create conda environment
conda create -n mindscope python=3.10 -y
conda activate mindscope
# Install dependencies
pip install -r requirements.txt# Launch survey assessment app
streamlit run version1_survey/app/streamlit_app.py# Launch text analysis app
streamlit run version2_text/app/streamlit_text_app.pyTraditional machine learning approach using psychological questionnaire data to predict mental health risk based on social media usage patterns and psychological indicators.
- 481 survey responses from individuals aged 13-91
- 21 questions covering social media usage and psychological well-being
- Balanced classes: 54.1% low risk, 45.9% high risk
# Composite psychological scores
social_media_addiction_score = mean([aimless_use, distraction, restlessness])
social_comparison_score = mean([comparison, validation_seeking])
general_mental_health_score = mean([worry, sleep_issues, concentration])| Model | Accuracy | F1 Score | Key Strength |
|---|---|---|---|
| Optimized Random Forest | 75.3% | 76.0% | Feature importance |
| Voting Hard Ensemble | 73.2% | 74.0% | Robust predictions |
| SVM (Original) | 72.5% | 72.5% | Stable baseline |
| Gradient Boosting | 68.0% | 68.7% | Non-linear patterns |
- Hyperparameter optimization with RandomizedSearchCV
- Polynomial feature engineering for non-linear relationships
- Feature selection using statistical tests
- Ensemble methods for improved robustness
# Interactive survey assessment
python version1_survey/notebooks/02_data_preprocessing.py
python version1_survey/notebooks/03_model_training.py
python version1_survey/notebooks/04_model_optimization.pyModern NLP approach using transformer models to analyze social media text for mental health risk indicators.
- 100 authentic-style mental health posts
- Balanced classes: 50 high risk, 50 low risk samples
- Realistic social media language patterns
# DistilBERT fine-tuning setup
model = DistilBertForSequenceClassification.from_pretrained(
'distilbert-base-uncased',
num_labels=2
)
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')- 3 epochs with early stopping
- AdamW optimizer (lr=2e-5)
- Batch size: 8
- Max sequence length: 128 tokens
- 80/20 train/validation split
| Metric | Score | Details |
|---|---|---|
| Accuracy | 90.0% | 18/20 correct predictions |
| F1 Score | 88.9% | Balanced precision/recall |
| Precision (High Risk) | 100% | No false positives |
| Recall (Low Risk) | 100% | No missed low-risk cases |
- Attention visualization for model interpretability
- Real-time text analysis with confidence scoring
- Feature extraction (distress keywords, sentiment)
- Transformer fine-tuning on domain-specific data
# Text analysis pipeline
python version2_text/notebooks/01_reddit_data_collection.py
python version2_text/notebooks/02_distilbert_text_classification.py
# Make predictions
classifier = DistilBertMentalHealthClassifier()
prediction, confidence, probs = classifier.predict_text(
"I feel overwhelmed and can't sleep. Everything seems pointless."
)
# Output: HIGH RISK (77.9% confidence)- Feature importance rankings identify key psychological indicators
- SHAP values explain individual predictions
- Correlation analysis reveals relationships between mental health factors
- Attention weights show which words influenced decisions
- Keyword analysis identifies distress vs. positive language patterns
- Token-level visualization highlights important phrases
# Example attention visualization
tokens = ['I', 'feel', 'overwhelmed', 'and', 'hopeless']
attention = [0.1, 0.3, 0.8, 0.2, 0.9] # Higher = more important| Aspect | Survey-Based | Text Classification |
|---|---|---|
| Accuracy | 75.3% | 90.0% |
| Data Type | Structured survey | Unstructured text |
| Model Type | Random Forest | DistilBERT |
| Interpretability | Feature importance | Attention weights |
| Real-time Use | Questionnaire required | Instant text analysis |
| Scalability | Limited by surveys | Scalable to any text |
Features:
- Interactive psychological questionnaire
- Real-time risk assessment
- Feature importance visualization
- Demographic analysis
Features:
- Free-text input for any message
- Instant mental health risk assessment
- Attention visualization showing important words
- Example texts and usage guidance
- Data Collection: Kaggle mental health survey dataset
- Feature Engineering: Composite psychological scores
- Model Selection: 4 algorithms with cross-validation
- Optimization: Hyperparameter tuning + ensembles
- Evaluation: Comprehensive performance metrics
- Data Collection: Authentic mental health text generation
- Preprocessing: Tokenization with DistilBERT tokenizer
- Model Training: Fine-tuning with custom dataset
- Evaluation: Attention analysis + performance metrics
- Deployment: Real-time prediction interface
# Survey-Based (Random Forest)
Accuracy: 75.3%
Precision: [79%, 72%] # [Low Risk, High Risk]
Recall: [63%, 80%]
F1-Score: [70%, 76%]
# Text Classification (DistilBERT)
Accuracy: 90.0%
Precision: [83%, 100%] # [Low Risk, High Risk]
Recall: [100%, 80%]
F1-Score: [91%, 89%]- Survey: 5-fold CV shows consistent 75% Β± 3% accuracy
- Text: Limited data but strong train/validation agreement
- Clear disclaimers that models are not medical diagnosis tools
- Privacy protection with anonymized data collection
- Bias monitoring across demographic groups
- Crisis resources provided in all interfaces
- Educational purpose only - not a replacement for professional help
- Dataset size - models trained on limited samples
- Cultural context - may not generalize across all populations
- Temporal factors - mental health changes over time
- Larger datasets for improved generalization
- Multi-class classification (anxiety, depression, etc.)
- Confidence calibration for better uncertainty estimation
- A/B testing of different model architectures
- Longitudinal analysis tracking changes over time
- Multimodal inputs (text + behavioral data)
- Personalized interventions based on risk factors
- Clinical validation studies with healthcare providers
- Devlin et al. (2018) - BERT: Pre-training of Deep Bidirectional Transformers
- Sanh et al. (2019) - DistilBERT, a distilled version of BERT
- Coppersmith et al. (2015) - CLPsych: Quantifying Mental Health Signals in Twitter
- CLPsych Shared Tasks - Mental health detection benchmarks
- Kaggle Social Media & Mental Health - Survey-based analysis
- Reddit Mental Health Communities - Authentic discussion data
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
pytest tests/
# Code formatting
black mindscope/
flake8 mindscope/This project is licensed under the MIT License - see the LICENSE file for details.
Important: MindScope is designed for educational and research purposes only. It should not be used as a substitute for professional mental health diagnosis, treatment, or advice. If you or someone you know is experiencing mental health concerns, please seek help from qualified healthcare providers.
Crisis Resources:
- National Suicide Prevention Lifeline: 988
- Crisis Text Line: Text HOME to 741741
- International: befrienders.org
Built with β€οΈ for mental health awareness and AI education