0

I have an list of arrays, I want to reshape each array in the list and then stack.

A sample below

C = np.array([[-127, -108, -290],
       [-123,  -83, -333],
       [-126,  -69, -354],
       [-146, -211, -241],
       [-151, -209, -253],
       [-157, -200, -254]])



D = np.array([[-129, -146, -231],
       [-127, -148, -238],
       [-132, -157, -231],
       [ -93, -355, -112],
       [ -95, -325, -137],
       [ -99, -282, -163]])



E = np.array(([[-141, -133, -200],
       [-132, -123, -202],
       [-119, -117, -204],
       [-107, -210, -228],
       [-101, -194, -243],
       [-105, -175, -244]]))


ArrayList = (C,D,E)

to reshape an individual array I do the following

newArray = ArrayList[0].reshape(1,-1)


and produces the desired result

array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
        -241, -151, -209, -253, -157, -200, -254]])

I tried writing a for loop to go through each item

newArray = []
for i in ArrayList:
    i.reshape(1,-1)
    newArray.append(i)
    

But I got the same product as what I started with. The desired output is shown below

(array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
         -241, -151, -209, -253, -157, -200, -254]]),
 array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
         -112,  -95, -325, -137,  -99, -282, -163]]),
 array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
         -228, -101, -194, -243, -105, -175, -244]]))

Any help appreciated.

1
  • Is your desired result multidimensional or flattened? The double [ are a red flag. You also might check out numpy hstack and vstack Commented Mar 27, 2022 at 2:19

3 Answers 3

2

I think you just need to do this:

newArray = []
for i in ArrayList:
    j = i.reshape(1,-1)
    newArray.append(j)

print(newArray)

Output:

[array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
        -241, -151, -209, -253, -157, -200, -254]]), 
array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
        -112,  -95, -325, -137,  -99, -282, -163]]), 
array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
        -228, -101, -194, -243, -105, -175, -244]])]
Sign up to request clarification or add additional context in comments.

Comments

1

What about using a list comprehension?

newArray = [i.reshape(1,-1) for i in ArrayList]

Output:

[array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
         -241, -151, -209, -253, -157, -200, -254]]),
 array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
         -112,  -95, -325, -137,  -99, -282, -163]]),
 array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
         -228, -101, -194, -243, -105, -175, -244]])]

1 Comment

thats a nice one liner
0

reshape() does not guarantee that it will return a "view", as it is called in the docs, to the same array. See the docs for this. It could be a copy of the original array, hence you need to use the return value and append the return value to your array. This should fix your problem.

import numpy as np

C = np.array([[-127, -108, -290],
       [-123,  -83, -333],
       [-126,  -69, -354],
       [-146, -211, -241],
       [-151, -209, -253],
       [-157, -200, -254]])



D = np.array([[-129, -146, -231],
       [-127, -148, -238],
       [-132, -157, -231],
       [ -93, -355, -112],
       [ -95, -325, -137],
       [ -99, -282, -163]])



E = np.array(([[-141, -133, -200],
       [-132, -123, -202],
       [-119, -117, -204],
       [-107, -210, -228],
       [-101, -194, -243],
       [-105, -175, -244]]))


ArrayList = (C,D,E)

newArray = []
for i in ArrayList:
    newArray.append(i.reshape(1,-1))

print(newArray)

Expected output

[array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
        -241, -151, -209, -253, -157, -200, -254]]), array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
        -112,  -95, -325, -137,  -99, -282, -163]]), array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
        -228, -101, -194, -243, -105, -175, -244]])]

1 Comment

i.reshape(1,-1) doesn't work in-place, copy or view.

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.