GWPCA¶
This page documents 1 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
GWPCA¶
Fit a basic geographically weighted principal component analysis.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GWPCA |
| Signature | GWPCA(n_components: 'int' = 2, kernel: 'str \| Any' = 'bisquare', bandwidth: 'float \| int \| str \| None' = 'cv', adaptive: 'bool' = True, scaling: 'bool' = True, compute_scores: 'bool' = False, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/09_gwpca.py |
GWPCA ¶
GWPCA(
n_components: int = 2,
kernel: str | Any = "bisquare",
bandwidth: float | int | str | None = "cv",
adaptive: bool = True,
scaling: bool = True,
compute_scores: bool = False,
verbose: bool = False,
)
Fit a basic geographically weighted principal component analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of local principal components to retain. |
2
|
kernel
|
str | Any
|
Spatial kernel name or callable accepted by
:func: |
'bisquare'
|
bandwidth
|
float | int | str | None
|
Positive fixed distance or, when |
'cv'
|
adaptive
|
bool
|
Whether the bandwidth represents a nearest-neighbour count. |
True
|
scaling
|
bool
|
Whether to globally standardize variables before local PCA. When false, variables are globally centered only. Both paths then apply local weighted centering, matching GWmodel. |
True
|
compute_scores
|
bool
|
Whether to retain locally centered scores for all observations receiving positive weight at each evaluation location. |
False
|
verbose
|
bool
|
Whether to print a compact completion message. |
False
|
Notes
At evaluation location :math:u, basic GWPCA decomposes
.. math::
\sqrt{W(u)}\{X^* - \bar{X}_w(u)\} = U D V^T,
where :math:X^* is the globally centered or standardized matrix.
Local loadings are columns of :math:V, and local component variances
are :math:D^2 / \sum_i w_i(u).
Principal-component signs are mathematically indeterminate. pyGWRx applies a deterministic convention: the largest absolute loading in each component is made positive.
Source code in src/pygwrx/models/gwpca.py
select_bandwidth ¶
Select a fixed or adaptive bandwidth by leave-one-out CV.
Source code in src/pygwrx/models/gwpca.py
fit ¶
fit(
X: ndarray | DataFrame,
coords: ndarray | DataFrame,
eval_coords: ndarray | DataFrame | None = None,
compute_cv: bool = False,
) -> "GWPCA"
Fit local principal components at observation or evaluation locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray | DataFrame
|
Numeric matrix with observations in rows and variables in columns. |
required |
coords
|
ndarray | DataFrame
|
Observation coordinates with the same row count as |
required |
eval_coords
|
ndarray | DataFrame | None
|
Optional coordinates at which local loadings are
evaluated. The observations in |
None
|
compute_cv
|
bool
|
Whether to retain leave-one-out reconstruction-error contributions for the selected or supplied bandwidth. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
GWPCA |
'GWPCA'
|
The fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inputs, bandwidth, or local windows are invalid. |
Source code in src/pygwrx/models/gwpca.py
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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
transform ¶
Project rows using loadings already calibrated at matching locations.
This method does not interpolate or borrow the nearest loading surface.
To score new locations, first fit with those locations in eval_coords.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray | DataFrame
|
Rows to project, one row per fitted evaluation location. |
required |
coords
|
ndarray | DataFrame | None
|
Optional coordinates identifying the fitted evaluation locations. Every row must exactly match one fitted location. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
ndarray
|
Locally centered component scores. |
Source code in src/pygwrx/models/gwpca.py
get_winning_variable ¶
Return the largest-absolute-loading variable index per location.
Source code in src/pygwrx/models/gwpca.py
to_frame ¶
Return local variance proportions and winning PC1 variable.
Source code in src/pygwrx/models/gwpca.py
summary ¶
Return global and local variance diagnostics as a plain-text table.
Source code in src/pygwrx/models/gwpca.py
Runnable examples used on this page¶
examples/models/09_gwpca.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit GWPCA, inspect local loadings, and transform observations."""
# 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 GWPCA
X, _, coords = spatial_regression(n=48, p=3)
model = GWPCA(n_components=2, bandwidth=24, adaptive=True).fit(
X, coords, compute_cv=True
)
print_model_result(model)
print("scores_shape=", model.transform(X, coords).shape)
print("explained_variance_first_location=", model.local_pv_[0])