numpy view a nested array of arrays into a 1D array

Viewed 511

1) Is it possible to numpy view a nested array of arrays (with different shapes) into a 1D array:

Input:

from numpy import array as arr
a = arr([arr([arr([2,3]), arr([1])]), arr([5, 6, 7])])

Output:

arr([2, 3, 1, 5, 6, 7])

2. Is it possible to do so with and without creating a new vector (for example np.astype vs np.view)?

2 Answers

Examine the resulting array:

In [354]: a = np.array([np.array([np.array([2,3]), np.array([1])]), np.array([5,
     ...:  6, 7])])
<ipython-input-354-0bcb7871bdd4>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  a = np.array([np.array([np.array([2,3]), np.array([1])]), np.array([5, 6, 7])])
In [355]: a.shape
Out[355]: (2,)
In [356]: a[0].shape
Out[356]: (2,)
In [357]: a[1].shape
Out[357]: (3,)

It contains two elements, both arrays. One has 2 elements, the other 3. But the first itself contains 2 arrays.

We can join the 2 outer arrays with concatenate:

In [359]: np.concatenate(a)
Out[359]: array([array([2, 3]), array([1]), 5, 6, 7], dtype=object)

but the result is still object dtype because of the inner arrays.

We need to first clean up the inner array:

In [362]: a[0] = np.concatenate(a[0])
In [363]: a
Out[363]: array([array([2, 3, 1]), array([5, 6, 7])], dtype=object)
In [364]: np.concatenate(a)
Out[364]: array([2, 3, 1, 5, 6, 7])

This a new array, not a view. The original array is object dtype. The result is integer dtype. There's no way to make that conversion as a view.

numpy.reshape() is the function you're looking for. It does create a new vector, but there are no workarounds that I know of to get rid of that. If you know the size of your input array, a 1D equivalent for it would be:

output_array = numpy.reshape(input_array, dim)

where dim is the number of elements in input_array. For your particular example, the display of the array would be:

print(numpy.reshape(a, 6))

Note that numpy.reshape() can be used for shapes other than 1D

Related