I would like to use NumPy arrays as keys where each key will have a set of NumPy arrays as a value. I am wondering what is the most efficient way of doing this operation (i..e, inserting, searching).
my_keys = np.random.rand(5, 2)
my_keys
array([[0.05152605, 0.12405425],
[0.44344738, 0.87479441],
[0.39542315, 0.2788064 ],
[0.470308 , 0.73640885],
[0.58107681, 0.42968256]])
my_values = np.random.rand(10, 2)
my_values
array([[0.96544233, 0.38396357],
[0.55453457, 0.83432399],
[0.8736443 , 0.0506048 ],
[0.98617731, 0.41264541],
[0.76856053, 0.23441502],
[0.06770689, 0.27087991],
[0.29328327, 0.57327051],
[0.31798657, 0.11341894],
[0.76256025, 0.08786568],
[0.71370639, 0.30637008]])
Suppose my first key is [0.05152605, 0.12405425], I want the first two rows of my_values to be the value as [[0.96544233, 0.38396357],[0.55453457, 0.83432399]]
There is a post here for a similar question, but there is no code snippet. For example, I've tried what is offered as
my_array = numpy.arange(4).reshape((2,2))
my_dict = {}
my_dict[my_array.tobytes()] = None
however, it did not work for me.
np.isclose(orallclose) when comparing float arrays, due to the inherent precision limits of floats. Even if 2 arrays areallclose, they could produce differenttobytesstrings and dict keys.arrays. I thought that usingdictwould be the way to go.adict['foobar'], quite another to useadict[list(adict.keys)[3]]to access the array corresponding to the 4th key vector.