GWLasso¶
This page documents 1 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
GWLasso¶
Geographically weighted Lasso regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GWLasso |
| Signature | GWLasso(kernel: 'Union[str, Callable]' = 'exponential', bandwidth: 'Union[float, int, str, None]' = 'cv', alpha: 'AlphaLike' = 'cv', alpha_grid: 'Optional[Sequence[float]]' = None, n_alphas: 'int' = 30, alpha_min_ratio: 'float' = 0.001, cv_folds: 'int' = 5, standardize: 'bool' = True, adaptive: 'bool' = False, bandwidth_range: 'Optional[Tuple[float, float]]' = None, n_bandwidths: 'int' = 8, max_iter: 'int' = 5000, tol: 'float' = 1e-06, active_tol: 'float' = 1e-08, fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', random_state: 'Optional[int]' = 0, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/07_gw_lasso.py |
GWLasso ¶
GWLasso(
kernel: Union[str, Callable] = "exponential",
bandwidth: Union[float, int, str, None] = "cv",
alpha: AlphaLike = "cv",
alpha_grid: Optional[Sequence[float]] = None,
n_alphas: int = 30,
alpha_min_ratio: float = 0.001,
cv_folds: int = 5,
standardize: bool = True,
adaptive: bool = False,
bandwidth_range: Optional[Tuple[float, float]] = None,
n_bandwidths: int = 8,
max_iter: int = 5000,
tol: float = 1e-06,
active_tol: float = 1e-08,
fit_intercept: bool = True,
distance_metric: str = "euclidean",
random_state: Optional[int] = 0,
verbose: bool = False,
)
Bases: BaseSpatialRegressor
Geographically weighted Lasso regression.
At evaluation location :math:s, the model solves
.. math::
\frac{1}{2\sum_i w_i(s)}\sum_i w_i(s)
\left(y_i-\beta_0(s)-x_i^T\beta(s)\right)^2
+ \lambda(s)\|\beta^*(s)\|_1,
where :math:\beta^* denotes coefficients on locally standardised
predictors. The intercept is never penalised. alpha="cv" selects a
separate local penalty at every calibration or prediction location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Union[str, Callable]
|
Spatial kernel name or callable. Wheeler's original implementation used an exponential kernel; all standard pyGWRx kernels are supported. |
'exponential'
|
bandwidth
|
Union[float, int, str, None]
|
Fixed distance, adaptive neighbour count, or a selection token.
Use |
'cv'
|
alpha
|
AlphaLike
|
Non-negative fixed Lasso penalty, or |
'cv'
|
alpha_grid
|
Optional[Sequence[float]]
|
Optional descending or ascending positive penalty candidates.
When omitted, a local logarithmic path is generated from |
None
|
n_alphas
|
int
|
Number of generated local penalty candidates. |
30
|
alpha_min_ratio
|
float
|
Smallest generated penalty as a fraction of |
0.001
|
cv_folds
|
int
|
Number of deterministic shuffled folds for local penalty selection. |
5
|
standardize
|
bool
|
Standardise predictors using local weighted means and scales. |
True
|
adaptive
|
bool
|
Interpret a numeric bandwidth as an integer neighbour count. |
False
|
bandwidth_range
|
Optional[Tuple[float, float]]
|
Optional lower and upper bounds for bandwidth selection. |
None
|
n_bandwidths
|
int
|
Number of grid candidates used for bandwidth CV. |
8
|
max_iter
|
int
|
Maximum coordinate-descent iterations for every local Lasso. |
5000
|
tol
|
float
|
Coordinate-descent convergence tolerance. |
1e-06
|
active_tol
|
float
|
Absolute coefficient threshold used for local variable selection. |
1e-08
|
fit_intercept
|
bool
|
Estimate an unpenalised local intercept. |
True
|
distance_metric
|
str
|
Distance metric used by pyGWRx. |
'euclidean'
|
random_state
|
Optional[int]
|
Seed used for reproducible local CV folds. |
0
|
verbose
|
bool
|
Print bandwidth and fitting progress. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
coef_ |
Local coefficient matrix with shape |
|
intercept_ |
Local intercept vector. |
|
alpha_ |
Locally selected penalty values. |
|
active_vars_ |
Active predictor indices at every location. |
|
selection_frequency_ |
Fraction of locations selecting each predictor. |
|
bandwidth_ |
Selected fixed distance or adaptive neighbour count. |
References
Wheeler, D. C. (2009). Simultaneous coefficient penalization and model selection in geographically weighted regression: The geographically weighted lasso. Environment and Planning A, 41(3), 722-742.
Mulot, M., & Erb, S. (2025). GWlasso: Geographically Weighted Lasso.
CRAN package version 1.0.2.
Source code in src/pygwrx/models/gw_lasso.py
fit ¶
fit(
X: Union[ndarray, DataFrame],
y: Union[ndarray, Series],
coords: Union[ndarray, DataFrame],
) -> "GWLasso"
Fit local geographically weighted Lasso models.
Source code in src/pygwrx/models/gw_lasso.py
predict_parameters ¶
Estimate local coefficients at arbitrary coordinates.
Source code in src/pygwrx/models/gw_lasso.py
predict ¶
Predict by calibrating a weighted Lasso at each new location.
Source code in src/pygwrx/models/gw_lasso.py
get_variable_importance ¶
Return local selection frequency for every predictor.
Source code in src/pygwrx/models/gw_lasso.py
to_frame ¶
Return fitted local coefficients, selections, and residuals.
Source code in src/pygwrx/models/gw_lasso.py
Runnable examples used on this page¶
examples/models/07_gw_lasso.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit geographically weighted Lasso with a fixed local penalty."""
# 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 GWLasso
X, y, coords = spatial_regression(n=48, p=3)
model = GWLasso(
bandwidth=24, adaptive=True, alpha=0.06, max_iter=1000, random_state=0
).fit(X, y, coords)
print_model_result(model)
print("selection_frequency=", model.selection_frequency_)
print("predictions=", model.predict(X.iloc[:3], coords.iloc[:3]))