LGGWR¶
This page documents 2 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
LGGWR¶
Latent-Geometry Geographically Weighted Regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import LGGWR |
| Signature | LGGWR(latent_dim: 'int' = 2, bandwidth: 'BandwidthLike' = None, adaptive: 'bool' = False, kernel: 'str' = 'gaussian', geometry: 'str' = 'joint', learning_rate: 'float' = 0.05, max_iter: 'int' = 100, tol: 'float' = 1e-06, lambda_reg: 'float' = 0.0, orthogonal_constraint: 'Optional[bool]' = None, grad_clip: 'float' = 10.0, patience: 'int' = 20, select_bandwidth: 'bool' = True, random_state: 'Optional[int]' = None, verbose: 'bool' = False, *, fit_intercept: 'bool' = True, standardize_geometry: 'bool' = True, initialization: 'str' = 'coordinate', n_restarts: 'int' = 1, scale_constraint: 'str' = 'frobenius', bandwidth_updates: 'int' = 1) -> 'None' |
| Maintained example | examples/models/18_lg_gwr.py |
LGGWR ¶
LGGWR(
latent_dim: int = 2,
bandwidth: BandwidthLike = None,
adaptive: bool = False,
kernel: str = "gaussian",
geometry: str = "joint",
learning_rate: float = 0.05,
max_iter: int = 100,
tol: float = 1e-06,
lambda_reg: float = 0.0,
orthogonal_constraint: Optional[bool] = None,
grad_clip: float = 10.0,
patience: int = 20,
select_bandwidth: bool = True,
random_state: Optional[int] = None,
verbose: bool = False,
*,
fit_intercept: bool = True,
standardize_geometry: bool = True,
initialization: str = "coordinate",
n_restarts: int = 1,
scale_constraint: str = "frobenius",
bandwidth_updates: int = 1
)
Latent-Geometry Geographically Weighted Regression.
For observation input :math:u_i=[s_i,a_i], joint LG-GWR learns a linear
map :math:z_i=A u_i and defines
.. math::
w_{ij}=K(\|z_i-z_j\|/h).
The map is trained against leave-one-out prediction error. The default
Frobenius-norm constraint fixes the otherwise unidentified global scale of
A; the bandwidth carries that scale. Consequently, ordinary L2
regularisation is allowed only when scale_constraint="none".
The separable form keeps geographic distance as one channel and learns an
attribute map :math:\zeta_i=B a_i for a second multiplicative channel,
.. math::
w_{ij}=K(d_{ij}^{geo}/h_g)K(\|\zeta_i-\zeta_j\|/h_a).
With :math:h_a=\infty, the separable model reduces exactly to geographic
GWR at the same geographic bandwidth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dim
|
int
|
Dimension of the learned latent space. |
2
|
bandwidth
|
BandwidthLike
|
Joint latent bandwidth. In separable mode, a two-item tuple
supplies |
None
|
adaptive
|
bool
|
Interpret a numeric joint bandwidth as a neighbour count and convert it once to a fixed latent distance. The analytical gradient itself is for a fixed distance bandwidth. |
False
|
kernel
|
str
|
|
'gaussian'
|
geometry
|
str
|
|
'joint'
|
fit_intercept
|
bool
|
Add an unpenalised local intercept. A legacy leading all-ones column is detected and removed before the intercept is added. |
True
|
standardize_geometry
|
bool
|
Centre coordinates and scale them by one common factor (preserving geographic shape), and z-standardise attributes. |
True
|
initialization
|
str
|
|
'coordinate'
|
n_restarts
|
int
|
Number of deterministic restarts. The first uses the requested initialisation; later restarts are random. |
1
|
learning_rate
|
float
|
NumPy Adam learning rate. |
0.05
|
max_iter
|
int
|
Maximum iterations per geometry/bandwidth stage. |
100
|
tol
|
float
|
Improvement and convergence tolerance. |
1e-06
|
lambda_reg
|
float
|
Frobenius L2 regularisation. It must be zero while a norm or orthogonality constraint is active because the norm is then fixed. |
0.0
|
scale_constraint
|
str
|
|
'frobenius'
|
orthogonal_constraint
|
Optional[bool]
|
Deprecated compatibility switch. |
None
|
grad_clip
|
float
|
Global gradient-norm clipping threshold. |
10.0
|
patience
|
int
|
Early-stopping patience. |
20
|
select_bandwidth
|
bool
|
Select reporting bandwidth(s) by AICc. |
True
|
bandwidth_updates
|
int
|
Number of additional geometry-training stages after AICc bandwidth reselection. A value of one implements geometry -> bandwidth -> geometry -> bandwidth. |
1
|
random_state
|
Optional[int]
|
Reproducibility seed. |
None
|
verbose
|
bool
|
Print optimisation progress. |
False
|
Source code in src/pygwrx/models/lg_gwr.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 | |
fit ¶
fit(
X: ArrayLike,
y: VectorLike,
coords: ArrayLike,
attributes: Optional[ArrayLike] = None,
) -> "LGGWR"
Fit LG-GWR and return self.
Source code in src/pygwrx/models/lg_gwr.py
predict_result ¶
predict_result(
X: ArrayLike,
coords: ArrayLike,
attributes: Optional[ArrayLike] = None,
) -> LGGWRPredictionResult
Recalibrate local parameters at new locations.
Source code in src/pygwrx/models/lg_gwr.py
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 | |
predict ¶
Return LG-GWR predictions at new locations.
Source code in src/pygwrx/models/lg_gwr.py
results_frame ¶
Return training-location parameters, fitted values and latent coordinates.
Source code in src/pygwrx/models/lg_gwr.py
to_frame ¶
metric_frame ¶
Return rotation-invariant learned metric contributions.
Source code in src/pygwrx/models/lg_gwr.py
get_latent_coordinates ¶
get_latent_coordinates(
coords: Optional[ArrayLike] = None,
attributes: Optional[ArrayLike] = None,
) -> np.ndarray
Return training or transformed latent coordinates.
Source code in src/pygwrx/models/lg_gwr.py
summary ¶
Return a plain-text fitted-model summary.
Source code in src/pygwrx/models/lg_gwr.py
LGGWRPredictionResult¶
Detailed LG-GWR predictions at evaluation locations.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import LGGWRPredictionResult |
| Signature | LGGWRPredictionResult(predictions: 'np.ndarray', coefficients: 'np.ndarray', intercepts: 'np.ndarray', coords: 'np.ndarray', latent_coords: 'np.ndarray', feature_names: 'Tuple[str, ...]') -> None |
| Maintained example | examples/models/18_lg_gwr.py |
LGGWRPredictionResult
dataclass
¶
LGGWRPredictionResult(
predictions: ndarray,
coefficients: ndarray,
intercepts: ndarray,
coords: ndarray,
latent_coords: ndarray,
feature_names: Tuple[str, ...],
)
Detailed LG-GWR predictions at evaluation locations.
to_frame ¶
Return predictions and local parameters as a DataFrame.
Source code in src/pygwrx/models/lg_gwr.py
Runnable examples used on this page¶
examples/models/18_lg_gwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit latent-geometry GWR with auxiliary contextual attributes."""
# 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 latent_regression, print_model_result
from pygwrx import LGGWR, LGGWRPredictionResult
X, y, coords, attributes = latent_regression()
model = LGGWR(
latent_dim=2, bandwidth=2.5, select_bandwidth=False, max_iter=8, random_state=0
).fit(X, y, coords, attributes)
print_model_result(model)
print("latent_coordinates_shape=", model.latent_coords_.shape)
result = model.predict_result(X.iloc[:3], coords.iloc[:3], attributes.iloc[:3])
assert isinstance(result, LGGWRPredictionResult)
print(result.to_frame())