GWR¶
This page documents 2 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
GWR¶
Gaussian geographically weighted regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GWR |
| Signature | GWR(kernel: 'Union[str, Callable[[np.ndarray, float], np.ndarray]]' = 'gaussian', bandwidth: 'Union[float, int, str, None]' = 'cv', bandwidth_method: 'str' = 'cv', adaptive: 'bool' = False, bandwidth_range: 'Optional[Tuple[float, float]]' = None, optimization_method: 'str' = 'golden_section', fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', sigma2_v1: 'bool' = True, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/01_gwr.py |
GWR ¶
GWR(
kernel: Union[
str, Callable[[ndarray, float], ndarray]
] = "gaussian",
bandwidth: Union[float, int, str, None] = "cv",
bandwidth_method: str = "cv",
adaptive: bool = False,
bandwidth_range: Optional[Tuple[float, float]] = None,
optimization_method: str = "golden_section",
fit_intercept: bool = True,
distance_metric: str = "euclidean",
sigma2_v1: bool = True,
verbose: bool = False,
)
Bases: BaseSpatialRegressor
Gaussian geographically weighted regression.
At each target location :math:s_i, the model estimates
.. math::
\hat\beta(s_i) = (X^T W_i X)^{-1}X^T W_i y,
where W_i is produced by a spatial kernel. A fixed bandwidth is a
distance; an adaptive bandwidth is an integer neighbour count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Union[str, Callable[[ndarray, float], ndarray]]
|
Kernel name or callable accepting |
'gaussian'
|
bandwidth
|
Union[float, int, str, None]
|
Numeric bandwidth or automatic-selection criterion. If |
'cv'
|
bandwidth_method
|
str
|
Criterion used only when |
'cv'
|
adaptive
|
bool
|
Interpret the fitted bandwidth as an integer neighbour count. |
False
|
bandwidth_range
|
Optional[Tuple[float, float]]
|
User-specified search interval. Adaptive bounds must be integers. |
None
|
optimization_method
|
str
|
One-dimensional search method used by automatic bandwidth selection. |
'golden_section'
|
fit_intercept
|
bool
|
Include a local intercept. |
True
|
distance_metric
|
str
|
Metric forwarded to the core distance implementation. |
'euclidean'
|
sigma2_v1
|
bool
|
Residual-variance convention. |
True
|
verbose
|
bool
|
Print fit progress. |
False
|
Source code in src/pygwrx/models/gwr.py
fit ¶
fit(
X: Union[ndarray, DataFrame],
y: Union[ndarray, Series],
coords: Union[ndarray, DataFrame],
*,
compute_hat_matrix: bool = True,
compute_local_r2: bool = True,
compute_inference: bool = True,
compute_hat_matrix_flag: Optional[bool] = None,
verbose: Optional[bool] = None
) -> "GWR"
Fit the Gaussian GWR model and return self.
The smoother traces and influence values are always computed. Setting
compute_hat_matrix=False avoids storing the full n x n matrix while
retaining valid AIC/AICc/BIC, effective-parameter, residual-variance, and
influence diagnostics.
compute_hat_matrix_flag is retained as a compatibility alias for older
PyGWRx code. New code should use compute_hat_matrix.
Source code in src/pygwrx/models/gwr.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
get_local_parameters ¶
Return local intercepts and slopes at arbitrary coordinates.
Source code in src/pygwrx/models/gwr.py
get_local_coefficients ¶
summary ¶
Return a stable text summary of global and local model results.
Source code in src/pygwrx/models/gwr.py
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 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | |
GWRPredictionResult¶
Rich prediction result returned by :meth:GWR.predict_result.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GWRPredictionResult |
| Signature | GWRPredictionResult(predictions: 'np.ndarray', coef: 'np.ndarray', intercept: 'np.ndarray', coords: 'np.ndarray', feature_names: 'Tuple[str, ...]', coef_standard_errors: 'Optional[np.ndarray]' = None, intercept_standard_errors: 'Optional[np.ndarray]' = None, coef_t_values: 'Optional[np.ndarray]' = None, intercept_t_values: 'Optional[np.ndarray]' = None) -> None |
| Maintained example | examples/models/01_gwr.py |
GWRPredictionResult
dataclass
¶
GWRPredictionResult(
predictions: ndarray,
coef: ndarray,
intercept: ndarray,
coords: ndarray,
feature_names: Tuple[str, ...],
coef_standard_errors: Optional[ndarray] = None,
intercept_standard_errors: Optional[ndarray] = None,
coef_t_values: Optional[ndarray] = None,
intercept_t_values: Optional[ndarray] = None,
)
Rich prediction result returned by :meth:GWR.predict_result.
Runnable examples used on this page¶
examples/models/01_gwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Load a bundled real dataset, fit GWR, inspect it, and predict."""
from __future__ import annotations
# 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 pygwrx import GWR, GWRPredictionResult
from pygwrx.io import load_columbus
bundle = load_columbus(return_type="dict")
X = bundle["data"]
y = bundle["target"]
coords = bundle["coords"]
print("dataset=", bundle["description"])
print("features=", bundle["feature_names"])
print("license=", bundle["license"])
model = GWR(kernel="bisquare", bandwidth=24, adaptive=True).fit(X, y, coords)
print(model.summary())
print("score=", model.score(X, y, coords))
result = model.predict_result(X[:3], coords[:3])
assert isinstance(result, GWRPredictionResult)
print(result.to_frame())