Skip to content

Latest commit

Β 

History

History
139 lines (100 loc) Β· 4.36 KB

File metadata and controls

139 lines (100 loc) Β· 4.36 KB

Explainable GeoAI: Interpreting Socio-Spatial Patterns

An end-to-end spatial XAI pipeline combining XGBoost, SHAP, GeoShapley, and MGWR to uncover and visualize interpretable spatial effects in U.S. county-level voting data.


πŸ“‚ Repository Structure


explainable-geoai/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   β”œβ”€β”€ census/                   # raw ACS downloads
β”‚   β”‚   β”œβ”€β”€ shapefiles/              # county geometries
β”‚   β”‚   └── voting\_2021.csv          # raw vote share
β”‚   └── processed/
β”‚       β”œβ”€β”€ voting\_clean.csv         # cleaned tabular data
β”‚       β”œβ”€β”€ voting\_features.csv      # with engineered features & spatial lags
β”‚       β”œβ”€β”€ xgb\_automl\_model.pkl     # trained FLAML+XGBoost model
β”‚       β”œβ”€β”€ shap\_explanations.csv    # SHAP outputs
β”‚       β”œβ”€β”€ geoshapley\_explanations.csv # GeoShapley outputs
β”‚       β”œβ”€β”€ mgwr\_coefficients.csv    # MGWR baseline
β”‚       β”œβ”€β”€ bootstrap\_shap\_stats.csv # SHAP uncertainty stats
β”‚       └── fairness\_metrics.csv     # spatial fairness gaps
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ data\_loader.py               # load & clean
β”‚   β”œβ”€β”€ feature\_engineering.py       # spatial lags, GeoDataFrame
β”‚   β”œβ”€β”€ model\_training.py            # FLAML + XGBoost training
β”‚   β”œβ”€β”€ shap\_explainer.py            # Kernel SHAP wrapper
β”‚   β”œβ”€β”€ geoshapley\_explainer.py      # GeoShapley computations
β”‚   β”œβ”€β”€ mgwr\_comparison.py           # MGWR baseline scripts
β”‚   β”œβ”€β”€ bootstrap\_uncertainty.py     # bootstrap SHAP stats
β”‚   β”œβ”€β”€ spatial\_fairness.py          # compute residual‐fairness
β”‚   └── config.py                    # paths & constants
β”œβ”€β”€ dashboard/
β”‚   └── app.py                       # Streamlit + Folium dashboard
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ implementation\_notes.md      # detailed pipeline doc
β”‚   └── paper\_summary.pdf            # summary of Li (2025) chapter
β”œβ”€β”€ README.md                        # this file
└── requirements.txt                 # pip dependencies

βš™οΈ Installation

  1. Clone repo
    git clone https://github.com/yourusername/explainable-geoai.git
    cd explainable-geoai

2. **Create & activate** a virtual environment

   ```bash
   python3 -m venv venv
   source venv/bin/activate   # macOS/Linux
   venv\Scripts\activate      # Windows
  1. Install dependencies

    pip install -r requirements.txt
  2. Download raw data

    • Place voting_2021.csv in data/raw/
    • Download ACS and shapefiles via src/download_census.py or manually.

πŸš€ Quick Start

  1. Data & features

    python src/data_loader.py
    python src/feature_engineering.py
  2. Train model

    python src/model_training.py
  3. Generate explanations

    python src/shap_explainer.py
    python src/geoshapley_explainer.py
    python src/mgwr_comparison.py
    python src/bootstrap_uncertainty.py
    python src/spatial_fairness.py
  4. Launch dashboard

    cd dashboard
    streamlit run app.py

πŸ“ Scripts & Modules

  • data_loader.py: cleans raw vote + ACS, saves voting_clean.csv.
  • feature_engineering.py: builds spatial lags, exports voting_features.csv.
  • model_training.py: uses FLAML to find best XGBoost; saves model.
  • shap_explainer.py: Kernel SHAP over FLAML model β†’ shap_explanations.csv.
  • geoshapley_explainer.py: computes GeoShapley components β†’ geoshapley_explanations.csv.
  • mgwr_comparison.py: fits MGWR baseline β†’ mgwr_coefficients.csv.
  • bootstrap_uncertainty.py: bootstraps SHAP β†’ bootstrap_shap_stats.csv.
  • spatial_fairness.py: calculates fairness gaps β†’ fairness_metrics.csv.
  • dashboard/app.py: interactive Streamlit + Folium map.

πŸ“Š Dashboard Overview

  • SHAP: county-level attributions, with uncertainty.
  • GeoShapley: decomposed intrinsic (GEO), main, and interaction effects.
  • MGWR/OLS: local regression coefficients for comparison.
  • Fairness: residual differences across demographic groups.
  • Download any CSV for offline analysis.

🧾 Citing

If you use this work, please cite:

Li, Ziqi (2025). Explainable AI in Spatial Analysis. In: Advances in Spatial Data Science, Springer.