SGTWR¶
This page documents 2 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
SGTWR¶
Spatiotemporal geographically weighted regression with similarity.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import SGTWR |
| Signature | SGTWR(spatial_bandwidth: 'SelectionValue' = 'aicc', *, temporal_bandwidth: 'SelectionValue' = 'aicc', adaptive: 'bool' = True, alpha: 'SelectionValue' = 'aicc', similarity_vars: 'Optional[Sequence[Union[int, str]]]' = None, standardize_similarity: 'bool' = True, spatial_bandwidth_candidates: 'Optional[Sequence[Number]]' = None, temporal_bandwidth_candidates: 'Optional[Sequence[Number]]' = None, alpha_candidates: 'Optional[Sequence[Number]]' = None, causal: 'bool' = False, time_unit: 'str' = 'auto', fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', sigma2_v1: 'bool' = True, ridge: 'float' = 0.0, store_weights: 'bool' = True, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/16_sgtwr.py |
SGTWR ¶
SGTWR(
spatial_bandwidth: SelectionValue = "aicc",
*,
temporal_bandwidth: SelectionValue = "aicc",
adaptive: bool = True,
alpha: SelectionValue = "aicc",
similarity_vars: Optional[
Sequence[Union[int, str]]
] = None,
standardize_similarity: bool = True,
spatial_bandwidth_candidates: Optional[
Sequence[Number]
] = None,
temporal_bandwidth_candidates: Optional[
Sequence[Number]
] = None,
alpha_candidates: Optional[Sequence[Number]] = None,
causal: bool = False,
time_unit: str = "auto",
fit_intercept: bool = True,
distance_metric: str = "euclidean",
sigma2_v1: bool = True,
ridge: float = 0.0,
store_weights: bool = True,
verbose: bool = False
)
Spatiotemporal geographically weighted regression with similarity.
The published spatiotemporal weight is
.. math::
W_{ST,ij}=\exp\left[-\frac{1}{2}\left(
\left(\frac{d^S_{ij}}{h^S_i}\right)^2+
\left(\frac{d^T_{ij}}{h^T}\right)^2\right)\right].
Static attribute similarity follows SGWR:
.. math::
W_{S,ij}=\exp\left[-\left(
\frac{1}{m}\sum_{k=1}^{m}|z_{ik}-z_{jk}|\right)^2\right].
The final local weight is
.. math::
W_{ij}=\alpha W_{ST,ij}+(1-\alpha)W_{S,ij}.
The paper uses a genetic algorithm to tune the spatial neighbour count, temporal bandwidth, and mixing coefficient. pyGWRx uses a deterministic AICc candidate search so fitted results are reproducible and testable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_bandwidth
|
SelectionValue
|
Fixed spatial distance or adaptive neighbour count.
|
'aicc'
|
temporal_bandwidth
|
SelectionValue
|
Positive temporal bandwidth. |
'aicc'
|
adaptive
|
bool
|
Interpret |
True
|
alpha
|
SelectionValue
|
Spatiotemporal contribution in |
'aicc'
|
similarity_vars
|
Optional[Sequence[Union[int, str]]]
|
Predictor names or indices used for similarity.
|
None
|
standardize_similarity
|
bool
|
Standardize selected variables before taking absolute attribute differences. This matches the paper's Z-score preprocessing and the standardized SGWR implementation. |
True
|
spatial_bandwidth_candidates
|
Optional[Sequence[Number]]
|
Optional spatial candidates for AICc selection. |
None
|
temporal_bandwidth_candidates
|
Optional[Sequence[Number]]
|
Optional temporal candidates for AICc selection. |
None
|
alpha_candidates
|
Optional[Sequence[Number]]
|
Optional mixing candidates for AICc selection. |
None
|
causal
|
bool
|
Exclude source observations later than a regression time. |
False
|
time_unit
|
str
|
Numeric unit or datetime conversion convention used by GTWR. |
'auto'
|
fit_intercept
|
bool
|
Include a local intercept. |
True
|
distance_metric
|
str
|
Spatial distance metric supported by pyGWRx. |
'euclidean'
|
sigma2_v1
|
bool
|
Use |
True
|
ridge
|
float
|
Non-negative stabilization added to slope normal equations. The intercept is not penalized. |
0.0
|
store_weights
|
bool
|
Store component and combined weight matrices. |
True
|
verbose
|
bool
|
Print selected parameters and AICc. |
False
|
References
Li, M., Du, W., Yu, S., Hong, Z., Zhang, D., He, Y., & De, L. (2025). SGTWR Model with Spatial-Temporal Heterogeneity and Attribute Similarity for Urban Traffic Carbon Emission Driver Analysis. Sustainability, 17(23), 10773.
Source code in src/pygwrx/models/sgtwr.py
fit ¶
fit(
X: Union[ndarray, DataFrame],
y: Union[ndarray, Series],
coords: Union[ndarray, DataFrame],
times: object,
) -> "SGTWR"
Fit SGTWR at observed space-time locations.
Source code in src/pygwrx/models/sgtwr.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | |
predict_result ¶
predict_result(
X: Union[ndarray, DataFrame],
coords: Union[ndarray, DataFrame],
times: object,
) -> SGTWRPredictionResult
Recalibrate SGTWR at new space-time locations.
Source code in src/pygwrx/models/sgtwr.py
predict ¶
predict(
X: Union[ndarray, DataFrame],
coords: Union[ndarray, DataFrame],
times: object,
) -> np.ndarray
Return SGTWR predictions at new space-time locations.
Source code in src/pygwrx/models/sgtwr.py
get_results ¶
Return fitted values and local coefficients as a DataFrame.
Source code in src/pygwrx/models/sgtwr.py
SGTWRPredictionResult¶
Detailed predictions from a fitted SGTWR model.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import SGTWRPredictionResult |
| Signature | SGTWRPredictionResult(predictions: 'np.ndarray', coef: 'np.ndarray', intercept: 'np.ndarray', coords: 'np.ndarray', times: 'np.ndarray', feature_names: 'Tuple[str, ...]') -> None |
| Maintained example | examples/models/16_sgtwr.py |
SGTWRPredictionResult
dataclass
¶
SGTWRPredictionResult(
predictions: ndarray,
coef: ndarray,
intercept: ndarray,
coords: ndarray,
times: ndarray,
feature_names: Tuple[str, ...],
)
Detailed predictions from a fitted SGTWR model.
to_frame ¶
Return predictions and local parameters as a DataFrame.
Source code in src/pygwrx/models/sgtwr.py
Runnable examples used on this page¶
examples/models/16_sgtwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit similarity and geographically-temporally weighted regression."""
# 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, temporal_regression
from pygwrx import SGTWR, SGTWRPredictionResult
X, y, coords, times = temporal_regression(n=48, p=3)
model = SGTWR(
spatial_bandwidth=24,
temporal_bandwidth=2.0,
adaptive=True,
alpha=0.5,
similarity_vars=["x1", "x2"],
store_weights=True,
).fit(X, y, coords, times)
print_model_result(model)
print("combined_weights_shape=", model.combined_weights_.shape)
result = model.predict_result(X.iloc[:3], coords.iloc[:3], times[:3])
assert isinstance(result, SGTWRPredictionResult)
print(result.to_frame())