geovalidate.knn_range

geovalidate.knn_range(X, y, max_k=30)[source]

Estimate the number of nearest neighbours at which spatial autocorrelation drops to zero.

For k = 1, 2, …, max_k, computes the Pearson correlation between y and its k-NN spatial lag (the mean of each observation’s k nearest neighbours). Returns the last k at which the correlation is still positive, interpolating linearly between that k and the next to find the fractional crossing (then rounding down).

Parameters:
X : GeoDataFrame | GeoSeries | (n, 2) ndarray

Locations. 1-D time-series inputs are not supported.

y : array-like of shape (n,)

Observed values.

max_k : int, default 30

Maximum k to test.

Returns:

Estimated k at the autocorrelation range. Returns max_k if correlation never drops to zero within the tested range.

Return type:

int

Examples

>>> k = knn_range(gdf, y)
>>> from libpysal.graph import Graph
>>> graph = Graph.build_knn(gdf, k=k)
>>> lb = LocalBootstrap(graph=graph)