Skip to main content

Life Satisfaction Prediction using Machine Learning and SHAP

Project description

What Drives Life Satisfaction?

A Machine Learning & Explainable AI Study


Project Overview

This project explores the key factors that influence life satisfaction using a large-scale social survey dataset.(TGSS2024 https://www.tgss.org.tr/iletisim?type=dataset)

By combining machine learning models with explainable AI (SHAP), the goal is not only to predict life satisfaction but also to understand why people feel more or less satisfied with their lives.

This project includes a command-line interface (CLI) that allows you to run the full machine learning pipeline without modifying code.

You can:

  • Train different models
  • Save and load trained models
  • Export results as JSON
  • Control plot generation
  • Reproduce experiments easily

Objectives

  • Predict individual life satisfaction (lifesat)
  • Identify the most important social, economic, and psychological factors
  • Provide interpretable insights using explainable AI techniques

Dataset

The dataset contains hundreds of variables related to:

  • Demographics (age, gender, education)
  • Social trust and relationships
  • Work and income
  • Religion and beliefs
  • Health and well-being
  • Political and social attitudes

⚠️ The dataset required extensive preprocessing due to:

  • Special missing values (-90, -88, -99)
  • High dimensionality (500+ columns)

Data Preprocessing

Key steps:

  • Converted special values (-90, -88, -99) → NaN
  • Removed columns with high missing rates
  • Imputed remaining missing values using median
  • Reduced dimensionality through feature engineering

Feature Engineering

Raw survey data was transformed into meaningful indices:

  • Social Trust Index → trust in people, fairness, neighbors
  • Discrimination Index → perceived discrimination across groups
  • Work Stress Index → stress, fatigue, work-life balance
  • Religiosity Score → religious practices and beliefs
  • Social Activity Score → frequency of social interactions
  • Economic Satisfaction → income and job satisfaction

This reduced 500+ variables into a compact, interpretable feature set.


How To Run

All commands should be executed from the project root:

python -m src.cli

Available Models

You can choose between:

  • linear → Linear Regression
  • rf → Random Forest (default)
  • xgb → XGBoost

CLI Usage Examples

🔹 Train a model

python -m src.cli --model-type rf

🔹 Train and save model

python -m src.cli --model-type rf --save-model

🔹 Save results as JSON

python -m src.cli --save-json

🔹 Save both model and results

python -m src.cli --model-type xgb --save-model --save-json

🔹 Load a trained model

python -m src.cli --load-model --model-path models/model.pkl

🔹 Load results from JSON

python -m src.cli --load-json

🔹 Disable plots

python -m src.cli --no-plot

🔹 Custom output paths

python -m src.cli \
  --model-type xgb \
  --save-model \
  --save-json \
  --model-path models/xgb_model.pkl \
  --json-path models/xgb_results.json \
  --output-dir results/

How It Works

  • Loads dataset from data/
  • Cleans and preprocesses data
  • Trains or loads a model
  • Evaluates performance (MAE)

Saves:

  • Model (.pkl)
  • Results (.json)
  • Plots (.png)
  • Output Files
  • Models
  • models/model.pkl
  • Results (JSON)
  • models/results.json

Example:

{
    "model_type": "rf",
    "mae": 0.85
}
  • Plots results/lifesat_distribution.png

Exploratory Data Analysis

Feature Importance

Feature Importance

Life Satisfaction Distribution

Life Satisfaction Distribution

Life Satisfaction by Gender

Life Satisfaction vs Gender

Life Satisfaction vs Income

Life Satisfaction vs Income

Life Satisfaction vs Social Trust

Life Satisfaction vs Social Trust

Life Satisfaction vs Work Stress

Life Satisfaction vs Work Stress

Life Satisfaction by Age

Life Satisfaction by Age

SHAP Summary

SHAP Summary

SHAP Feature Importance

SHAP Feature Importance

SHAP Individual Explanation

SHAP Waterfall

Machine Learning Models

Three models were trained and evaluated:

  • Linear Regression (baseline)
  • Random Forest Regressor
  • XGBoost Regressor

Model Performance (MAE)

Model MAE
Linear Regression 0.7439083518939844
Random Forest 0.7508413001912044
XGBoost 0.8352283612036112

👉 Tree-based models outperformed linear regression, indicating non-linear relationships in the data.


Feature Importance

Random Forest Feature Importance

Feature Importance


Explainability with SHAP

To understand model predictions, SHAP (SHapley Additive exPlanations) was used.

SHAP Summary Plot

SHAP Summary


SHAP Feature Importance

SHAP Feture Plot


Key Findings

  • Social trust is one of the strongest predictors of life satisfaction
  • Work stress has a significant negative impact
  • Economic satisfaction contributes positively but less than social factors
  • Social activity is strongly associated with higher well-being

👉 Overall, social and psychological factors outweigh purely economic ones


Conclusion

This project demonstrates that:

Life satisfaction is driven more by human relationships and trust than by material factors alone.

By combining machine learning with explainability, we can uncover meaningful insights about society—not just make predictions.


Technologies Used

  • Python
  • pandas, numpy
  • matplotlib, seaborn
  • scikit-learn
  • XGBoost
  • SHAP

📁 Project Structure

life-satisfaction-ml/
│
├── data/
├── notebooks/
├── results/
├── models/
├── src/
├── requirements.txt
└── README.md

Future Improvements

  • Hyperparameter tuning
  • Cross-validation
  • Deploying as a web app (Streamlit)
  • Interactive dashboard (Power BI / Tableau)

Author

Cetin ERDEM


⭐ If you found this project interesting

Feel free to ⭐ the repo and connect!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lifesat_ml-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lifesat_ml-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file lifesat_ml-0.1.0.tar.gz.

File metadata

  • Download URL: lifesat_ml-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lifesat_ml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1407126b79ac6ae237a413eaff834b46063bb1c6d371f097459a8b376894bd15
MD5 066474c8bad629d2acecf66ac738035a
BLAKE2b-256 71709990a77dd5e05c66f75c93f1ed4f4d4f5c63b0be8609117dabdeccb81cef

See more details on using hashes here.

File details

Details for the file lifesat_ml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lifesat_ml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lifesat_ml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a678f192c9e42aa4668b6b12d099ff0b11be3aa00347f30755d67da25e5f83e
MD5 3f996b9e45c9249777406857ac3e490b
BLAKE2b-256 b7e634229502d3ddf090f372716818cef9cfce3bf31121603afd838fddb4c5e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page