0

I have tried to add numpy arrays (multiple of numpy_array) in a for loop to a list where each numpy array might have varies shapes like (but let us say each has (10, 64)). So each array has 64 columns and might have as many rows as possible. What I did was the following:

l_arr = numpy_array[indices, :]
list_arr.append(l_arr.tolist())

Then,

numpy_from_list= np.array(list_arr)

But this seems to add extra dimension. For example If I added 3 numpy arrays each of size (10, 64) to a list and then convert a list to a numpy. I would get (3, 10, 64), but instead I want (30, 64).

for i in range(3):
    l_arr = numpy_array[indices, :]
    list_arr.append(l_arr.tolist())
    numpy_from_list= np.array(list_arr)

Can you please help me with that?

2
  • try to use numpy's np.stack() function Commented Jul 31, 2021 at 15:01
  • @sehan2. Thank you, but I got same result. Commented Jul 31, 2021 at 15:04

1 Answer 1

1

Here you go:

arrays = [np.random.randint(0,10,(10, 64)) for i in range(3)]
res = np.vstack(arrays)
Sign up to request clarification or add additional context in comments.

6 Comments

While running the loop, how I can set a condition that size of stacked arrays should not exceed 30 for example? I did your code and it works, but it has issues as the list size of 3 has actual array sizes of 30 by 64. So I can not set a condition to stop the loop based on list size like while len(list_numpy_arryas) <=30:
I am adding arrays iteratively not in a single call as you did with arrays above.
your arrays have to have compatible dimensions to stack them
They are not compatible unfortunatley. One might have (10, 64) while another might have (3, 64), etc. So I first added them to a list and once I finish I stacked them as you did, but the issue is I want to stop once I added a total of (64, 64) from all arrays. Can you help me with that please?
look into 'list comprehensions' or use a for loop as you did above
|

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.