The sparse matrix is ββthe first solution that came to mind, but since X and Y are floating, it's a little messy:
In [624]: I=((X-.4)*10).round().astype(int) In [625]: J=((Y-1)*10).round().astype(int) In [626]: I,J Out[626]: (array([0, 1, 0, 0, 3]), array([0, 0, 1, 2, 2])) In [627]: sparse.coo_matrix((Z,(J,I))).A Out[627]: array([[ 3.3, 2.5, 0. , 0. ], [ 3.6, 0. , 0. , 0. ], [ 3.8, 0. , 0. , 1.8]])
It is still necessary, one way or another, to compare these coordinates with the indices [0,1,2 ...]. My quick trick was simply to scale the values ββlinearly. However, I had to take care to convert float to ints.
sparse.coo_matrix works because the natural way to define a sparse matrix is (i, j, data) tuples, which of course can be translated into lists I , J , Data or arrays.
I rather like the solution for the story, although I did not have the opportunity to use it.