1

I am trying to loop through a set of coordinates and 'stacking' these arrays of coordinates to another array (so in essence I want to have an array of arrays) using numpy.

This is my attempt:

import numpy as np
all_coordinates = np.array([[]])

for y in range(2):
    for x in range(2):
        coordinate = np.array([[x,y]])

        # append
        all_coordinates = np.append(all_coordinates,[coordinate])

print(all_coordinates)

But it's not working. It's just concatenating the individual numbers and not appending the array.

Instead of giving me (the output that I want to achieve):

[[0 0] [1 0] [0,1] [1,1]]

The output I get instead is:

[0 0 1 0 0 1 1 1]

Why? What I am doing wrong here?

4
  • Likely a duplicate post of this out there, but take a look at np.stack or np.concatenate. Commented May 23, 2022 at 18:08
  • If I use np.stack I get "ValueError: all input arrays must have the same shape". Can't keep stacking these arrays in runtime using np.stack. Commented May 23, 2022 at 18:11
  • Stack needs like-sized arrays, so something like np.stack([(x, y) for x in range(2) for y in range(2)]) may be a suitable option for you. I don't think re-stacking after creating each coordinate will work. If there is a specific reason that does not work for you, concatenate may be a better option. Commented May 23, 2022 at 18:16
  • 1
    Do not use np.append in loops, this is very inefficient (both space and time). Use a list and np.concatenate/np.stack/np.hstack/np.vstack. By the way, you can reshape your output: all_coordinates.reshape(-1, 2). In fact, the aforementioned does that internally. Additionally, you can preallocate the array to the right shape directly and assign lines. This is more space-efficient and it should also be faster. Commented May 23, 2022 at 18:24

2 Answers 2

2

The problem that stack functions don't work, is that they need that the row added is of the same size of the already present rows. Using np.array([[]]), the first row is has a length of zero, which means that you can only add rows that also have length zero.

In order to solve this, we need to tell Numpy that the first row is of size two and not zero. The array thus needs to be of size (0, 2) and not (0, 0). This can be done using one of the array-initializing functions that accept size arguments, like empty, zeros or ones. Which function does not matter, as there are no spaces to fill.

Then you can use one of the functions mentioned in comments, like vstack or stack. The code thus becomes:

import numpy as np
all_coordinates = np.zeros((0, 2))

for y in range(2):
    for x in range(2):
        coordinate = np.array([[x,y]])

        # append
        all_coordinates = np.vstack((all_coordinates, coordinate))

print(all_coordinates)
Sign up to request clarification or add additional context in comments.

4 Comments

Initializing the array with a (0, 2) shape does not solve the problem of the OP. You should try it.
It allows the usage of vstack, and thus solves the problem. I have tried that.
vstack works if the parameter are modified (ie. np.vstack((all_coordinates,coordinate))) which is certainly not obvious for the OP as for stack this is even more non-trivial. I would be good to add an example of code for the OP and future readers.
Th OP modified the parameters because append was the only function that worked. He clearly knows how to use stack and vstack, as he said in comments that it raised an error. Anyway, I added an example.
0

In such a case, I would use a list and only convert it into an array once you have appended all the elements you want. here is a suggested improvement

import numpy as np
all_coordinates = []

for y in range(2):
    for x in range(2):
        coordinate = np.array([x,y])

        # append
        all_coordinates.append(coordinate)

all_coordinates = np.array(all_coordinates)

print(all_coordinates)

The output of this code is indeed

array([[0, 0],
       [1, 0],
       [0, 1],
       [1, 1]])

Comments

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.