Skip to content Skip to sidebar Skip to footer

Saving A Sequence Of 3rd-order Tensors And Reading It Back Without Losing Array Format

Python 3.7, Numpy: I need to save a 3rd-order object, which was created using numpy. It is a list of arrays, to be precise. The arrays get matrix-multiplied to vectors using numpy.

Solution 1:

Here is a way to save the arrays as a dict but not as list (because saving it as list concatenates all the arrays into a single one, which we don't want) and then load it back for reading without losing the array format.

# sample array to work with
In [76]: arr = np.arange(12).reshape(4, 3)

# make a dict of say 4 copies of the array
In [77]: dict_of_arrs = {idx: arr for idx in range(4)}

# serialize it to disk; will be saved as `serialized_arrays.npy`
In [78]: np.save('serialized_arrays', dict_of_arrs)

# load it back for reading/processing
In [79]: loaded_arrs = np.load('serialized_arrays.npy')

# flatten it out and just take the 0th element in the list.
In [80]: loaded_arrs.ravel()[0]
Out[80]: 
{0: array([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]]), 1: array([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]]), 2: array([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]]), 3: array([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])}

The above will return a dict; You can then iterate over this dict and access the arrays. If you prefer, you can give some sensible keys when making the dict dict_of_arrs.

Post a Comment for "Saving A Sequence Of 3rd-order Tensors And Reading It Back Without Losing Array Format"