2

Given three lists x,y,z of identical size à la

x = [1, 0,.2,.2, 1, 0]
y = [0, 0, 0, 1,.2,.2]
z = [0, 2, 3, 1, 0, 1]

with unique but incomplete pairings of x,y float values, how to map z to a matrix Z[i,j] where i,j correspond to the indices np.unique of x,y respectively? In the example this would be something like

Z = [[ 2,  0,  3],
     ['', '',  1],
     [ 1,  0, '']]

where '' might as well be np.nan. This does somehow sound like an inverse np.meshgrid, and I could hack up my own implementation, but is there no pre-existing solution?

I tried the suggestions here, but they assume a complete grid. Another solution sounds nice but interpolates the missing points, which is not what I want.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • First, rename Z to R (otherwise you have two z variables). Then Initialize `R` as a matrix of `Nan`, then loop through `x` and assign the `z[i]` value to the `R[x[i],y[i]]` coordinate –  Mar 21 '17 at 10:07
  • @SembeiNorimaki Thanks, that's about what I would have done. But fortunately Divakar knows [a simpler way](https://stackoverflow.com/a/42923907/321973) – Tobias Kienzler Mar 21 '17 at 10:09

2 Answers2

4

One approach would be -

m,n = np.max(x)+1, np.max(y)+1    
out = np.full((m,n), np.nan)
out[x,y] = z

Sample run -

In [213]: x = [4,0,2,2,1,0]
     ...: y = [0,0,0,1,2,5]
     ...: z = [0,2,3,1,0,1]
     ...: 

In [214]: m,n = np.max(x)+1, np.max(y)+1    
     ...: out = np.full((m,n), np.nan)
     ...: out[x,y] = z
     ...: 

In [215]: out
Out[215]: 
array([[  2.,  nan,  nan,  nan,  nan,   1.],
       [ nan,  nan,   0.,  nan,  nan,  nan],
       [  3.,   1.,  nan,  nan,  nan,  nan],
       [ nan,  nan,  nan,  nan,  nan,  nan],
       [  0.,  nan,  nan,  nan,  nan,  nan]])

For floating point values, we could use np.unique(..return_inverse) to give each of the X's and Y's unique int IDs, which could be used as row and column indices for indexing into output array -

x_arr = np.unique(x, return_inverse=1)[1]
y_arr = np.unique(y, return_inverse=1)[1]

m,n = np.max(x_arr)+1, np.max(y_arr)+1    
out = np.full((m,n), np.nan)
out[x_arr,y_arr] = z

Sample run -

In [259]: x = [1, 0,.2,.2, 1, 0]
     ...: y = [0, 0, 0, 1,.2,.2]
     ...: z = [0, 2, 3, 1, 0, 1]
     ...: 

In [260]: x_arr = np.unique(x, return_inverse=1)[1]
     ...: y_arr = np.unique(y, return_inverse=1)[1]
     ...: 
     ...: m,n = np.max(x_arr)+1, np.max(y_arr)+1    
     ...: out = np.full((m,n), np.nan)
     ...: out[x_arr,y_arr] = z
     ...: 

In [261]: out
Out[261]: 
array([[  2.,   1.,  nan],
       [  3.,  nan,   1.],
       [  0.,   0.,  nan]])
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • Awesome, I didn't know indexing in numpy can do that :) **edit** Ok, small catch, this only works for integer `x,y`s... – Tobias Kienzler Mar 21 '17 at 10:10
  • Sorry, I should not have used `0,1,2` as example values when they might as well be arbitrary floats... Still, this is a good starting hint, I'll think about it some more – Tobias Kienzler Mar 21 '17 at 10:17
  • OK, for floats, replace `out[x,y]` by `out[xi,yi]` where `xi = np.asarray([np.arange(len(ux))[ux==v] for v in x]).flatten()` with `ux = np.unique(x)` and same for `yi`. Though I guess my construct can be optimized... – Tobias Kienzler Mar 21 '17 at 10:25
  • @TobiasKienzler Added a modified one for floating point values. – Divakar Mar 21 '17 at 10:31
1

Based on Divakar's answer, but also working for non-index x,ys:

ux, xi = np.unique(x, return_inverse=1)
uy, yi = np.unique(y, return_inverse=1)
X, Y = np.meshgrid(ux, uy)
Z = np.full(X.shape, np.nan)
Z[xi, yi] = z
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221