Skip to main content

a map tool

Project description

xtg · 扩展型地理加权回归工具箱

Python-ready GWR / MGWR / AGWR with bandwidth search, diagnostics & KNN-GWR.

xtg 致力于把 R 生态里的地理加权回归(GWR)全家桶完整迁移到 Python, 并补充了 自适应 KNN-GWR、自动带宽搜索、VIF / Moran’s I 诊断等常用功能。

功能 说明
OLS / GWR / SGWR / MGWR / AGWR 基准 → 单带宽 → 全局+局部 → 多带宽 → 双向各向异性
KNN-GWR 自适应样本数带宽,适合离散、非平稳数据
带宽搜索 1D select_bw_iso (自动 bounded)
ND select_bw_aniso (L-BFGS-B / Powell)
诊断指标 vif(多重共线性) · moran(全局 Moran’s I)
评估指标 rmsepseudo_r2aicc
核函数 高斯 / 双二次 · 各向同性 + 各向异性 · 可扩展
稀疏权重矩阵 core.weights.compute_weights → SciPy CSR

依赖安装

pip install xtg                       # 最新发布版
# or
pip install git+https://github.com/yourname/xtg.git  # 开发版

额外依赖:numpy · scipy · pandas · scikit-learn · pysal pip 会自动安装,若使用 KNN-GWR 不需额外包。


快速上手

0 准备数据

import pandas as pd, numpy as np
df = pd.read_csv("house_price_panel.csv")

y  = np.log1p(df["AUP"].values)                 # ← 建议对房价取 log
X  = df[["RRP", "AVPI"]].values                 # 共线性低的两列
coords = df[["lon", "lat"]].values
coords_std = (coords - coords.mean(0)) / coords.std(0)   # 标准化!

1 全局 OLS

from xtg import fit_ols
ols = fit_ols(X, y)
print("OLS RMSE", ols.rmse_, "R²≈", ols.pseudo_r2_)

2 单带宽 GWR(自动搜索带宽)

from xtg import fit_gwr, select_bw_iso

best_bw = select_bw_iso(
    lambda bw: fit_gwr(X, y, coords_std, bw=bw).aicc_,
    bounds=(0.5, 5)
)
gwr = fit_gwr(X, y, coords_std, bw=best_bw)
print("GWR RMSE", gwr.rmse_)

3 各向异性 AGWR(双带宽)

from xtg import fit_agwr, select_bw_aniso
best_bwx, best_bwy = select_bw_aniso(
    lambda b: fit_agwr(X, y, coords_std, bw=tuple(b)).aicc_,
    ndim=2,
    bounds=[(0.5, 5)]*2,
    bw_init=[2, 1.5],
)
agwr = fit_agwr(X, y, coords_std, bw=(best_bwx, best_bwy))
print("AGWR RMSE", agwr.rmse_)

4 自适应样本数 KNN-GWR

from xtg.models.gwr_knn import fit_gwr_knn
gwr_knn = fit_gwr_knn(X, y, coords_std, k=20)
print("KNN-GWR RMSE", gwr_knn["rmse"])

空间诊断一键跑

from xtg import vif, moran
print(vif(X, feature_names=["RRP", "AVPI"]))
mi = moran(y, coords=coords_std, k=8)
print(f"Moran's I = {mi['I']:.3f},  p = {mi['p_sim']:.3f}")

建议实践流程

  1. 坐标平面化 / 标准化 → 避免带宽量纲错配
  2. VIF < 10Moran’s I 显著 → 才值得做局部模型
  3. 搜索带宽select_bw_iso / select_bw_aniso 支持 AICc / CV
  4. 比较 OLS ↔ GWR ↔ AGWR ↔ KNN-GWR,选择最小 RMSE 模型
  5. .predict(X_new, coords_new) → 直接做空间外插预测

引用

如果 xtg 为你的研究或产品节省了时间,欢迎在论文 / 项目中引用:

Meng, J. (2025). xtg: An Extended Toolkit for Geographically Weighted Regression in Python. 
https://pypi.org/project/xtg/

许可证

Apache 2.0 | 欢迎 PR / Issue!


本包仍在快速演进中:计划引入 自适应 bisquare KNN-GWR局部 ridge 稳定化GPU 批量拟合,敬请关注 ⭐。

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

xtg-0.0.4.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

xtg-0.0.4-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file xtg-0.0.4.tar.gz.

File metadata

  • Download URL: xtg-0.0.4.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for xtg-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e7233fb3810cae655387d278bf51782fdbe7ed6e78f3cadb73f432b718cef5d2
MD5 caa03826e9fffa44bf63d8d4fce755ab
BLAKE2b-256 1fe3ff7ed3ff434454228409b66ff8d153665e44a7b0a7ed1875aaded0b915f9

See more details on using hashes here.

File details

Details for the file xtg-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: xtg-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for xtg-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 183b1e00c10261ff1c8050cf38873884e17b2b47572ba5eec838ecf3f2eae482
MD5 06eeb2609fb5c89d5fb690b5cbd448cc
BLAKE2b-256 6f7af8652944073647e6b6b2991389f5ebf35a5c116b9eb06d264a18e732925c

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