0

I am basically trying to achieve this, but need the unfolding to be done in a different fashion. i want all samples of the N-1th dimension to be concatenated. For example, if my unfolding were to be applied to an RGB image of (100,100,3) the new array would basically become a (100,300) where the 3 colour channel images are now side by side in the new array.

All my attempts to use a neat built in numpy function like flatten and concatenate yielded no results. (flatten, because the end goal is to apply this unfolding until it is a 1D array)

Can't even think of a slicing way of doing it in a loop since the starting number of dimensions isn't constant (array = array[:,...,:,0]+...+array[:,...,:,0])

EDIT

I just came up with this way of achieving what I want, but would still welcome better, more pure, numpy solutions.

shape = numpy.random.randint(100, size=numpy.random.randint(100))
array = numpy.random.uniform(size=shape)

array = array.T
for i in range(0, len(shape)-1, -1):
    array = numpy.concatenate(array)
5
  • Why rgb_image.reshape(100, 300) doesn't do the job? Commented Feb 10, 2017 at 18:41
  • As mentioned, the image example is just an example. I need this behaviour to fold an N-Dimensional Array down to a 1D array recursively Commented Feb 10, 2017 at 18:43
  • 1
    "because the end goal is to apply this unfolding until it is a 1D array" - then why not just flatten your array to 1D directly? A single flatten or ravel call should be all you need. Commented Feb 10, 2017 at 18:44
  • Have you run the sample code that you have included in the question? It attempts to create a huge array. Commented Feb 10, 2017 at 18:58
  • yea its a randomly sized array, just change the 100 if you cannot run it on your device Commented Feb 10, 2017 at 19:04

1 Answer 1

0

Am I right in deducing that you want flatten the last 2 dimensions of the array?

In [96]: shape = numpy.random.randint(10, size=numpy.random.randint(10))+1
In [97]: shape
Out[97]: array([2, 7, 2])
In [98]: newshape=tuple(shape[:-2])+(-1,)
In [99]: arr = np.arange(np.prod(shape)).reshape(shape)
In [100]: arr.shape
Out[100]: (2, 7, 2)
In [101]: arr.reshape(newshape).shape
Out[101]: (2, 14)
In [102]: arr.reshape(newshape)
Out[102]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]])

If you don't like the order of terms in the last dimension, you may need to transpose

In [109]: np.swapaxes(arr, -1,-2).reshape(newshape)
Out[109]: 
array([[ 0,  2,  4,  6,  8, 10, 12,  1,  3,  5,  7,  9, 11, 13],
       [14, 16, 18, 20, 22, 24, 26, 15, 17, 19, 21, 23, 25, 27]])

I can't test it against your code because range(0,len(shape)-1, -1) is an empty range.

I don't think you want

In [112]: np.concatenate(arr,axis=-1).shape
Out[112]: (7, 4)
In [113]: np.concatenate((arr[0,...],arr[1,...]), axis=-1)
Out[113]: 
array([[ 0,  1, 14, 15],
       [ 2,  3, 16, 17],
       [ 4,  5, 18, 19],
       [ 6,  7, 20, 21],
       [ 8,  9, 22, 23],
       [10, 11, 24, 25],
       [12, 13, 26, 27]])

That splits the arr on the 1st axis, and then joins it on the last.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, not quite what i wanted, but it gave me some hints on my quest. my solutions does achieve what i want. I guess there is no 1 numpy function that achieves what I want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.