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.
CodePudding user response:
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]])]
CodePudding user response:
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]])]
CodePudding user response:
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]])]