GWDA¶
This page documents 1 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
GWDA¶
Fit geographically weighted linear or quadratic discriminant analysis.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import GWDA |
| Signature | GWDA(kernel: 'str \| Any' = 'bisquare', bandwidth: 'float \| int \| str \| None' = 'cv', adaptive: 'bool' = True, quadratic: 'bool' = False, local_mean: 'bool' = True, local_cov: 'bool' = True, local_prior: 'bool' = True, prior: 'np.ndarray \| list[float] \| tuple[float, ...] \| None' = None, regularization: 'float' = 0.0, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/10_gwda.py |
GWDA ¶
GWDA(
kernel: str | Any = "bisquare",
bandwidth: float | int | str | None = "cv",
adaptive: bool = True,
quadratic: bool = False,
local_mean: bool = True,
local_cov: bool = True,
local_prior: bool = True,
prior: (
ndarray | list[float] | tuple[float, ...] | None
) = None,
regularization: float = 0.0,
verbose: bool = False,
)
Fit geographically weighted linear or quadratic discriminant analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
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 |
True
|
quadratic
|
bool
|
Whether to use class-specific covariance matrices (WQDA). The default uses a locally pooled covariance matrix (WLDA). |
False
|
local_mean
|
bool
|
Whether class means vary geographically. |
True
|
local_cov
|
bool
|
Whether class covariance matrices vary geographically. |
True
|
local_prior
|
bool
|
Whether class prior probabilities vary geographically. |
True
|
prior
|
ndarray | list[float] | tuple[float, ...] | None
|
Optional fixed class priors in sorted class-label order. Values must be non-negative and sum to one. |
None
|
regularization
|
float
|
Explicit non-negative ridge added to covariance diagonals. The default performs the published unregularized method and raises when a required covariance is singular. |
0.0
|
verbose
|
bool
|
Whether to print a compact completion message. |
False
|
Notes
For class :math:g at prediction location :math:u, pyGWRx computes
a local weighted mean :math:\mu_g(u), an unbiased weighted covariance
:math:\Sigma_g(u), and a local prior :math:\pi_g(u). The Gaussian
discriminant cost is
.. math::
d_g(x,u) = \tfrac12\log|\Sigma_g(u)|
+ \tfrac12(x-\mu_g(u))^T\Sigma_g(u)^{-1}(x-\mu_g(u))
- \log\pi_g(u).
WLDA replaces the class-specific covariance by a locally pooled covariance. Classification selects the class with minimum cost.
GWmodel::gwda uses the same local statistics and classification
ordering, but its published R source multiplies a matrix-norm term by
the number of classes. pyGWRx uses the standard Gaussian log-determinant
formula so that returned probabilities have a clear statistical meaning.
Source code in src/pygwrx/models/gwda.py
select_bandwidth ¶
select_bandwidth(
X: ndarray | DataFrame,
y: ndarray | Series,
coords: ndarray | DataFrame,
*,
bounds: tuple[float | int, float | int] | None = None
) -> float | int
Select a bandwidth by maximizing leave-one-out accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray | DataFrame
|
Training feature matrix. |
required |
y
|
ndarray | Series
|
Training class labels. |
required |
coords
|
ndarray | DataFrame
|
Training coordinates. |
required |
bounds
|
tuple[float | int, float | int] | None
|
Optional closed search interval. Adaptive bounds are integer neighbour counts; fixed bounds are positive distances. |
None
|
Returns:
| Type | Description |
|---|---|
float | int
|
Selected bandwidth. |
Source code in src/pygwrx/models/gwda.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
fit ¶
fit(
X: ndarray | DataFrame,
y: ndarray | Series,
coords: ndarray | DataFrame,
X_pred: ndarray | DataFrame | None = None,
coords_pred: ndarray | DataFrame | None = None,
validate: bool = True,
) -> "GWDA"
Fit GWDA and optionally evaluate training or supplied prediction rows.
When prediction rows are omitted, validate=True performs leave-one-out
classification and stores accuracy and a GWmodel-style confusion matrix.
Supplying X_pred and coords_pred performs ordinary prediction while
retaining the fitted training data for later calls to :meth:predict.
Source code in src/pygwrx/models/gwda.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 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 | |
predict ¶
Predict class labels at new spatial locations without refitting.
predict_proba ¶
Return normalized Gaussian class probabilities at new locations.
Source code in src/pygwrx/models/gwda.py
predict_entropy ¶
Return normalized Shannon classification entropy at new locations.
get_entropy ¶
summary ¶
Return a plain-text fitted-model summary.
Source code in src/pygwrx/models/gwda.py
Runnable examples used on this page¶
examples/models/10_gwda.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit geographically weighted discriminant analysis."""
# 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 classification_data
from pygwrx import GWDA
X, y, coords = classification_data()
model = GWDA(bandwidth=28, adaptive=True, quadratic=False).fit(X, y, coords)
print(model.summary())
print("classes=", model.classes_)
print("predictions=", model.predict(X.iloc[:5], coords.iloc[:5]))
print("probabilities=", model.predict_proba(X.iloc[:5], coords.iloc[:5]))