How do I convert the numpy record array below:
recs = [('Bill', 31, 260.0), ('Fred', 15, 145.0)]
r = rec.fromrecords(recs, names='name, age, weight', formats='S30, i2, f4')
to a list of dictionary like:
[{'name': 'Bill', 'age': 31, 'weight': 260.0},
'name': 'Fred', 'age': 15, 'weight': 145.0}]
We can convert the Numpy array to the list by tolist() method, we can have a list of data element which is converted from an array using this method.
np. save is designed to save numpy arrays. data is a dictionary. So it wrapped it in a object array, and used pickle to save that object.
Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster. So overall a task executed in Numpy is around 5 to 100 times faster than the standard python list, which is a significant leap in terms of speed.
I am not sure there is built-in function for that or not, but following could do the work.
>>> [dict(zip(r.dtype.names,x)) for x in r]
[{'age': 31, 'name': 'Bill', 'weight': 260.0},
{'age': 15, 'name': 'Fred', 'weight': 145.0}]
This depends on the final structure required. This example shows a numpy recarray composed of several 1D subsets. To have a python dictionary instead, a possible conversion is:
import numpy as np
a = np.rec.array([np.array([1,3,6]), np.array([3.4,4.2,-1.2])], names=['t', 'x'])
b = {name:a[name] for name in a.dtype.names}
Here's a solution that works in the following cases not covered by the other answers:
np.array((1, 2), dtype=[('a', 'float32'), ('b', 'float32')])
np.void
(result of indexing a record array)structured_to_dict(np.zeros((), dtype=[('a', [('b', 'float32', (2,))])]))
)def structured_to_dict(arr: np.ndarray):
import numpy as np
if np.ndim(arr) == 0:
if arr.dtype.names == None:
return arr.item()
# accessing by int does *not* work when arr is a zero-dimensional array!
return {k: structured_to_dict(arr[k]) for k in arr.dtype.names}
return [structured_to_dict(v) for v in arr]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With