Unsuccessful append to an empty NumPy array Unsuccessful append to an empty NumPy array arrays arrays

Unsuccessful append to an empty NumPy array


I might understand the question incorrectly, but if you want to declare an array of a certain shape but with nothing inside, the following might be helpful:

Initialise empty array:

>>> a = np.zeros((0,3)) #or np.empty((0,3)) or np.array([]).reshape(0,3)>>> aarray([], shape=(0, 3), dtype=float64)

Now you can use this array to append rows of similar shape to it. Remember that a numpy array is immutable, so a new array is created for each iteration:

>>> for i in range(3):...     a = np.vstack([a, [i,i,i]])...>>> aarray([[ 0.,  0.,  0.],       [ 1.,  1.,  1.],       [ 2.,  2.,  2.]])

np.vstack and np.hstack is the most common method for combining numpy arrays, but coming from Matlab I prefer np.r_ and np.c_:

Concatenate 1d:

>>> a = np.zeros(0)>>> for i in range(3):...     a = np.r_[a, [i, i, i]]...>>> aarray([ 0.,  0.,  0.,  1.,  1.,  1.,  2.,  2.,  2.])

Concatenate rows:

>>> a = np.zeros((0,3))>>> for i in range(3):...     a = np.r_[a, [[i,i,i]]]...>>> aarray([[ 0.,  0.,  0.],       [ 1.,  1.,  1.],       [ 2.,  2.,  2.]])

Concatenate columns:

>>> a = np.zeros((3,0))>>> for i in range(3):...     a = np.c_[a, [[i],[i],[i]]]...>>> aarray([[ 0.,  1.,  2.],       [ 0.,  1.,  2.],       [ 0.,  1.,  2.]])


numpy.append is pretty different from list.append in python. I know that's thrown off a few programers new to numpy. numpy.append is more like concatenate, it makes a new array and fills it with the values from the old array and the new value(s) to be appended. For example:

import numpyold = numpy.array([1, 2, 3, 4])new = numpy.append(old, 5)print old# [1, 2, 3, 4]print new# [1, 2, 3, 4, 5]new = numpy.append(new, [6, 7])print new# [1, 2, 3, 4, 5, 6, 7]

I think you might be able to achieve your goal by doing something like:

result = numpy.zeros((10,))result[0:2] = [1, 2]# Orresult = numpy.zeros((10, 2))result[0, :] = [1, 2]

Update:

If you need to create a numpy array using loop, and you don't know ahead of time what the final size of the array will be, you can do something like:

import numpy as npa = np.array([0., 1.])b = np.array([2., 3.])temp = []while True:    rnd = random.randint(0, 100)    if rnd > 50:        temp.append(a)    else:        temp.append(b)    if rnd == 0:         break result = np.array(temp)

In my example result will be an (N, 2) array, where N is the number of times the loop ran, but obviously you can adjust it to your needs.

new update

The error you're seeing has nothing to do with types, it has to do with the shape of the numpy arrays you're trying to concatenate. If you do np.append(a, b) the shapes of a and b need to match. If you append an (2, n) and (n,) you'll get a (3, n) array. Your code is trying to append a (1, 0) to a (2,). Those shapes don't match so you get an error.


This error arise from the fact that you are trying to define an object of shape (0,) as an object of shape (2,). If you append what you want without forcing it to be equal to result[0] there is no any issue:

b = np.append([result[0]], [1,2])

But when you define result[0] = b you are equating objects of different shapes, and you can not do this. What are you trying to do?