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.
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
- 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
-
Install dependencies
pip install -r requirements.txt
-
Download raw data
- Place
voting_2021.csvindata/raw/ - Download ACS and shapefiles via
src/download_census.pyor manually.
- Place
-
Data & features
python src/data_loader.py python src/feature_engineering.py
-
Train model
python src/model_training.py
-
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
-
Launch dashboard
cd dashboard streamlit run app.py
data_loader.py: cleans raw vote + ACS, savesvoting_clean.csv.feature_engineering.py: builds spatial lags, exportsvoting_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.
- 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.
If you use this work, please cite:
Li, Ziqi (2025). Explainable AI in Spatial Analysis. In: Advances in Spatial Data Science, Springer.