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.
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]])
Based on Divakar's answer , but also working for non-index x,y
s:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.