MGWR¶
This page documents 1 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
MGWR¶
Gaussian multiscale geographically weighted regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import MGWR |
| Signature | MGWR(kernel: 'Union[str, Callable[[np.ndarray, float], np.ndarray]]' = 'bisquare', bandwidths: 'BandwidthInput' = None, bandwidth_method: 'str' = 'aicc', adaptive: 'bool' = True, bandwidth_range: 'BandwidthRange' = None, bandwidth_ranges: 'BandwidthRanges' = None, init_bandwidth: 'Optional[Bandwidth]' = None, optimization_method: 'str' = 'golden_section', search_tol: 'float' = 1e-06, search_max_iter: 'int' = 200, max_iter: 'int' = 200, tol: 'float' = 1e-05, rss_score: 'bool' = False, bws_same_times: 'int' = 5, fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', sigma2_v1: 'bool' = True, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/02_mgwr.py |
MGWR ¶
MGWR(
kernel: Union[
str, Callable[[ndarray, float], ndarray]
] = "bisquare",
bandwidths: BandwidthInput = None,
bandwidth_method: str = "aicc",
adaptive: bool = True,
bandwidth_range: BandwidthRange = None,
bandwidth_ranges: BandwidthRanges = None,
init_bandwidth: Optional[Bandwidth] = None,
optimization_method: str = "golden_section",
search_tol: float = 1e-06,
search_max_iter: int = 200,
max_iter: int = 200,
tol: float = 1e-05,
rss_score: bool = False,
bws_same_times: int = 5,
fit_intercept: bool = True,
distance_metric: str = "euclidean",
sigma2_v1: bool = True,
verbose: bool = False,
)
Bases: BaseMultiscaleRegressor
Gaussian multiscale geographically weighted regression.
MGWR represents the response as a sum of spatially varying additive terms, with one bandwidth for the intercept and each predictor when an intercept is fitted. The model is calibrated by iteratively updating one term at a time while holding the remaining additive terms fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Union[str, Callable[[ndarray, float], ndarray]]
|
Kernel name or callable accepting |
'bisquare'
|
bandwidths
|
BandwidthInput
|
Optional manual bandwidth or sequence of bandwidths. A
scalar is applied to every parameter. A sequence must contain one
value per fitted parameter, including the intercept when present.
If |
None
|
bandwidth_method
|
str
|
Criterion used for the initial GWR bandwidth and each
variable-specific bandwidth search. Supported values are |
'aicc'
|
adaptive
|
bool
|
Interpret bandwidths as integer nearest-neighbour counts. |
True
|
bandwidth_range
|
BandwidthRange
|
Optional common search range for all parameters. |
None
|
bandwidth_ranges
|
BandwidthRanges
|
Optional parameter-specific search ranges. Supply one range per fitted parameter, including the intercept when present. |
None
|
init_bandwidth
|
Optional[Bandwidth]
|
Optional bandwidth for the initial single-bandwidth GWR
fit. If |
None
|
optimization_method
|
str
|
One-dimensional bandwidth search method. |
'golden_section'
|
search_tol
|
float
|
Convergence tolerance used by variable-specific bandwidth searches. |
1e-06
|
search_max_iter
|
int
|
Maximum iterations for each variable-specific bandwidth search. |
200
|
max_iter
|
int
|
Maximum number of backfitting iterations. |
200
|
tol
|
float
|
Score-of-change convergence tolerance. |
1e-05
|
rss_score
|
bool
|
Use relative RSS change instead of smoothing-function change as the convergence score. |
False
|
bws_same_times
|
int
|
Stop repeating bandwidth searches after the complete bandwidth vector remains unchanged for this many iterations. |
5
|
fit_intercept
|
bool
|
Include a spatially varying intercept. |
True
|
distance_metric
|
str
|
Distance metric used to form spatial neighbourhoods. |
'euclidean'
|
sigma2_v1
|
bool
|
Residual-variance convention. |
True
|
verbose
|
bool
|
Print backfitting progress. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
bandwidths_ |
Optional[ndarray]
|
Final variable-specific bandwidth vector. |
bandwidth_history_ |
Optional[Any]
|
Bandwidth vector from every backfitting iteration. |
convergence_history_ |
Optional[Any]
|
Score of change from every iteration. |
initial_bandwidth_ |
Optional[Any]
|
Initial single-bandwidth GWR bandwidth. |
effective_params_by_variable_ |
Optional[Any]
|
Effective parameter count for each coefficient surface. |
coef_ |
Optional[Any]
|
Local slope estimates with shape
|
intercept_ |
Optional[Any]
|
Local intercept estimates with shape |
parameter_standard_errors_ |
Optional[Any]
|
Local standard errors for all fitted parameters. |
parameter_t_values_ |
Optional[Any]
|
Local t statistics for all fitted parameters. |
converged_ |
Optional[Any]
|
Whether the backfitting score reached |
n_iter_ |
Optional[Any]
|
Number of completed backfitting iterations. |
Notes
Out-of-sample MGWR prediction is intentionally not exposed because the
widely used reference Python implementation does not provide a validated
prediction algorithm for independently supplied target locations. Use
fitted_values_ for calibration-location estimates.
References
Fotheringham, A. S., Yang, W., and Kang, W. (2017). Multiscale geographically weighted regression (MGWR). Annals of the American Association of Geographers, 107(6), 1247-1265.
Source code in src/pygwrx/models/mgwr.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
fit ¶
fit(
X: Union[ndarray, DataFrame],
y: Union[ndarray, Series],
coords: Union[ndarray, DataFrame],
*,
compute_hat_matrix: bool = False,
store_partial_hat_matrices: bool = False,
compute_inference: bool = True,
n_chunks: int = 1,
verbose: Optional[bool] = None
) -> "MGWR"
Fit the MGWR model and return self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame]
|
Predictor matrix with shape |
required |
y
|
Union[ndarray, Series]
|
Response vector with shape |
required |
coords
|
Union[ndarray, DataFrame]
|
Coordinates with shape |
required |
compute_hat_matrix
|
bool
|
Retain the complete model smoother matrix. |
False
|
store_partial_hat_matrices
|
bool
|
Retain one |
False
|
compute_inference
|
bool
|
Compute local standard errors and t statistics. Exact smoother traces are computed regardless of this setting. |
True
|
n_chunks
|
int
|
Number of column chunks used during exact inference. |
1
|
verbose
|
Optional[bool]
|
Optional per-fit override of the estimator's verbosity. |
None
|
Returns:
| Type | Description |
|---|---|
'MGWR'
|
The fitted model instance. |
Source code in src/pygwrx/models/mgwr.py
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 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 | |
predict ¶
Reject unvalidated out-of-sample MGWR prediction.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. Use |
Source code in src/pygwrx/models/mgwr.py
to_frame ¶
Return calibration-location parameters and diagnostics.
Source code in src/pygwrx/models/mgwr.py
summary ¶
Return a stable text summary of MGWR calibration results.
Source code in src/pygwrx/models/mgwr.py
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 | |
Runnable examples used on this page¶
examples/models/02_mgwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit MGWR with fixed variable-specific bandwidths."""
# 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, spatial_regression
from pygwrx import MGWR
X, y, coords = spatial_regression(n=48, p=2)
model = MGWR(bandwidths=[24, 26, 28], adaptive=True, max_iter=8, tol=0.5).fit(
X, y, coords, compute_inference=True
)
print_model_result(model)
try:
model.predict(X.iloc[:2], coords.iloc[:2])
except NotImplementedError as exc:
print("Expected MGWR prediction limitation:", exc)