I here a 'self-containing' answer in the sense that I first generate some input data, which I then convert into a dictionary and then back into the original arrays. On the way, I add some random noise to keep the x and y values close to each other but still make them unique. Following this answer, a list of all values that are 'close' to each other can be found by first rounding the values and then using np.unique
.
mport numpy as np
##generating some input data:
print('input arrays')
xvals = np.linspace(1,10, 5)
print(xvals)
yvals = np.linspace(0.1, 0.4, 4)
print(yvals)
xvals, yvals = np.meshgrid(xvals, yvals)
##adding some noise to make it more interesting:
xvals += np.random.rand(*xvals.shape)*1e-3
yvals += np.random.rand(*yvals.shape)*1e-5
zvals = np.arange(xvals.size).reshape(*xvals.shape)
print(zvals)
input_dict ={
(i,j): k for i,j,k in zip(
list(xvals.flatten()), list(yvals.flatten()), list(zvals.flatten())
)
}
##print(input_dict)
x,y,z = map(np.array,zip(*((x,y,z) for (x,y),z in input_dict.items())))
##this part will need some tweaking depending on the size of your
##x and y values
xlen = len(np.unique(x.round(decimals=2)))
ylen = len(np.unique(y.round(decimals=3)))
x = x.round(decimals=2).reshape(ylen,xlen)[0,:]
y = y.round(decimals=3).reshape(ylen,xlen)[:,0]
z = z.reshape(ylen,xlen)
print('\n', 'output arrays')
print(x)
print(y)
print(z)
The output looks like this:
input arrays
[ 1. 3.25 5.5 7.75 10. ]
[0.1 0.2 0.3 0.4]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
output arrays
[ 1. 3.25 5.5 7.75 10. ]
[0.1 0.2 0.3 0.4]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
Old Answer:
There are a lot of assumptions in this answer, mainly because there is not quite enough information in the question. But, assuming that
- the x and y values are as nicely ordered as in the example data
- the x and y values are complete
One could go about the problem with a list comprehension and a reshaping of numpy ndarrays:
import numpy as np
input_dict = {
(0,0): 1,
(1,0): 2,
(0,1): 3,
(1,1): 4,
}
x,y,z = map(np.array,zip(*((x,y,z) for (x,y),z in input_dict.items())))
xlen = len(set(x))
ylen = len(set(y))
x = x.reshape(xlen,ylen)[0,:]
y = y.reshape(xlen,ylen)[:,0]
z = z.reshape(xlen,ylen)
print(x)
print(y)
print(z)
which gives
[0 1]
[0 1]
[[1 2]
[3 4]]
hope this helps.
PS: If the x and y values are not in necessarily in the order suggested by the posted example data, one can still solve the issue with some clever sorting.