79119191

Date: 2024-10-23 18:22:06
Score: 0.5
Natty:
Report link

The following uses sklearn.neighbors.KDTree to make fast queries from cell midpoints to data points. It yields the number of unique values present from a certain radius to the grid cell midpoints.

import numpy as np
from sklearn.neighbors import KDTree

n = 100_00
cell_size = 0.01
radius = 0.05


coords = np.random.random_sample((n, 2))
values = np.random.randint(0, n//10, n)

x_axis = np.linspace(0, 1, int(1 / cell_size))
y_axis = np.linspace(0, 1, int(1 / cell_size))
xv, yv = np.meshgrid(x_axis, y_axis)
tree = KDTree(coords)

results = tree.query_radius(list(zip(xv.ravel(), yv.ravel())), radius)
unique_results = [len(np.unique(values[indices])) for indices in results]
grid_values = np.array(unique_results).reshape((len(x_axis), len(x_axis)))

This yields an raster like this: enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Victor Savenije