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)
Answer from The_spider on Stack Overflowpython - How to append arrays to another numpy array? - Stack Overflow
Numpy Array Append
Issues with Optimization / Appending to a Numpy Array
Why Appending a number to a numpy array appends a dot?
Videos
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)
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]])
I'm trying to find out how to append a list to a Numpy array.
So I have a list of lists that I then convert into a Numpy array. I then want to append a list to that array.
So let's say I have
list = [[a,b,c],[b,c,d],[a,d,b]]
I first do:
list = np.array(list)
... (code that does calculations on that numpy array)
I then want to append:
list2 = [c,b,a]
to list.
I've tried np.index as well as np.append, however both of them give me the following output:
list = list = [[a,b,c],[b,c,d],[a,d,b,c,b,a]]
when I want:
list = [[a,b,c],[b,c,d],[a,d,b],[c,b,a]]
If this is too convoluted let me know, I can try explain it better.
I appreciate the help.
https://paste.ofcode.org/zpT6HK22LuswbXhTHZfUK6
I am working on a starter project which involves storing pixel coordinates for various "territories" (dictionaries) on a map. Because there are a huge amount of coordinates (33 million pixels), I have been trying to optimize the program so it uses less memory. I have been using numpy's int16 type and ndarray to save memory. The problem I am getting however, is with adding pixels to territories. Each territory (dictionary) has a "pixels" key with an array as a value, and to add pixels to that array I use the following method:
def addPixel(territory, pixel):
pixels = getProperty(territory, "pixels")
territory["pixels"] = numpy.append(pixels, pixel)This method, however, appears to be very slow, and it would take a huge amount of time to iterate over millions of pixels. Does anyone know of a faster way the append items to ndarrays?