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
yand 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:¶
- Returns:¶
Estimated k at the autocorrelation range. Returns max_k if correlation never drops to zero within the tested range.
- Return type:¶
Examples
>>> k = knn_range(gdf, y) >>> from libpysal.graph import Graph >>> graph = Graph.build_knn(gdf, k=k) >>> lb = LocalBootstrap(graph=graph)