GRGWR¶
This page documents 2 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
GRGWR¶
Geo-Regime Geographically Weighted Regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GRGWR |
| Signature | GRGWR(n_regimes: 'int' = 3, bandwidth: 'BandwidthLike' = 20, kernel: 'str' = 'bisquare', lambda_boundary: 'float' = 1.0, max_iter: 'int' = 10, tol: 'float' = 0.0001, spatial_constraint_weight: 'float' = 0.5, fit_intercept: 'bool' = True, verbose: 'bool' = False, *, n_neighbors: 'int' = 8, min_regime_size: 'Optional[int]' = None, enforce_connectivity: 'bool' = True, random_state: 'Optional[int]' = 42) -> 'None' |
| Maintained example | examples/models/19_gr_gwr.py |
GRGWR ¶
GRGWR(
n_regimes: int = 3,
bandwidth: BandwidthLike = 20,
kernel: str = "bisquare",
lambda_boundary: float = 1.0,
max_iter: int = 10,
tol: float = 0.0001,
spatial_constraint_weight: float = 0.5,
fit_intercept: bool = True,
verbose: bool = False,
*,
n_neighbors: int = 8,
min_regime_size: Optional[int] = None,
enforce_connectivity: bool = True,
random_state: Optional[int] = 42
)
Geo-Regime Geographically Weighted Regression.
GR-GWR models a piecewise-smooth coefficient field. An initial full-domain GWR provides local slope features. Spatially constrained agglomerative clustering produces connected initial regimes, and a sequential ICM update refines labels under
.. math::
L(z)=\sum_i(y_i-x_i^T\beta_i^{(z_i)})^2+\lambda B(z),
where :math:B(z) counts undirected neighbouring pairs with different
regime labels. Every accepted ICM move preserves the source regime's
connectivity and attaches the point to an adjacent target regime. A full
refit is accepted only when the reported objective does not increase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_regimes
|
int
|
Requested number of regimes. |
3
|
bandwidth
|
BandwidthLike
|
Positive fixed distance or one-based adaptive neighbour count. |
20
|
kernel
|
str
|
|
'bisquare'
|
lambda_boundary
|
float
|
Non-negative boundary-length penalty. |
1.0
|
max_iter
|
int
|
Maximum ICM sweeps. |
10
|
tol
|
float
|
Objective tolerance. |
0.0001
|
spatial_constraint_weight
|
float
|
:math: |
0.5
|
fit_intercept
|
bool
|
Add a local intercept. A legacy leading all-ones column is detected and removed. |
True
|
n_neighbors
|
int
|
k for the symmetric kNN adjacency graph. A minimum spanning tree is added so the graph is connected. |
8
|
min_regime_size
|
Optional[int]
|
Minimum members per regime. |
None
|
enforce_connectivity
|
bool
|
Preserve connected regimes during ICM. |
True
|
random_state
|
Optional[int]
|
Deterministic clustering and ICM order seed. |
42
|
verbose
|
bool
|
Print fitting progress. |
False
|
Notes
The reported AICc and ENP are conditional on the discovered regime labels. They measure the final piecewise local smoother and do not include the full discrete search complexity of regime discovery.
Source code in src/pygwrx/models/grgwr.py
fit ¶
Fit GR-GWR and return self.
Source code in src/pygwrx/models/grgwr.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
predict_result ¶
Assign query regimes and recalibrate local WLS coefficients.
Source code in src/pygwrx/models/grgwr.py
predict ¶
results_frame ¶
Return training regimes, local parameters and fitted values.
Source code in src/pygwrx/models/grgwr.py
to_frame ¶
select_parameters
classmethod
¶
select_parameters(
X: ArrayLike,
y: VectorLike,
coords: ArrayLike,
*,
n_regimes_grid: Tuple[int, ...] = (2, 3),
bandwidth_grid: Tuple[BandwidthLike, ...] = (20, 30),
lambda_boundary_grid: Tuple[float, ...] = (0.0, 1.0),
spatial_constraint_grid: Tuple[float, ...] = (
0.25,
0.5,
0.75,
),
criterion: str = "conditional_aicc",
cv_folds: int = 5,
random_state: Optional[int] = 42,
**model_kwargs: Any
) -> Tuple["GRGWR", pd.DataFrame]
Select a modest GR-GWR parameter grid and fit the best model.
criterion="conditional_aicc" compares final smoothers conditional
on their discovered labels. criterion="spatial_cv" forms compact
coordinate clusters and reports mean held-out squared error. The search
is intentionally explicit and exhaustive; users should keep the grids
small because every candidate contains a regime-discovery fit.
Returns:
| Type | Description |
|---|---|
Tuple['GRGWR', DataFrame]
|
|
Source code in src/pygwrx/models/grgwr.py
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | |
summary ¶
Return a plain-text fitted-model summary.
Source code in src/pygwrx/models/grgwr.py
GRGWRPredictionResult¶
Detailed GR-GWR predictions at evaluation locations.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GRGWRPredictionResult |
| Signature | GRGWRPredictionResult(predictions: 'np.ndarray', coefficients: 'np.ndarray', intercepts: 'np.ndarray', regimes: 'np.ndarray', coords: 'np.ndarray', feature_names: 'Tuple[str, ...]') -> None |
| Maintained example | examples/models/19_gr_gwr.py |
GRGWRPredictionResult
dataclass
¶
GRGWRPredictionResult(
predictions: ndarray,
coefficients: ndarray,
intercepts: ndarray,
regimes: ndarray,
coords: ndarray,
feature_names: Tuple[str, ...],
)
Detailed GR-GWR predictions at evaluation locations.
to_frame ¶
Return predictions, regimes and local parameters as a DataFrame.
Source code in src/pygwrx/models/grgwr.py
Runnable examples used on this page¶
examples/models/19_gr_gwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit geo-regime GWR and inspect connected spatial regimes."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
from _common import print_model_result, regime_regression
from pygwrx import GRGWR, GRGWRPredictionResult
X, y, coords, truth = regime_regression(n=56)
model = GRGWR(n_regimes=2, bandwidth=18, max_iter=2, random_state=0).fit(X, y, coords)
print_model_result(model)
print("regime_sizes=", model.regime_sizes_)
print(
"truth_agreement_or_label_swap=",
max((model.regimes_ == truth).mean(), (model.regimes_ != truth).mean()),
)
result = model.predict_result(X.iloc[:3], coords.iloc[:3])
assert isinstance(result, GRGWRPredictionResult)
print(result.to_frame())