How to Create an Empty Array and Then Append to It in Numpy

How to add a new row to an empty numpy array

The way to "start" the array that you want is:

arr = np.empty((0,3), int)

Which is an empty array but it has the proper dimensionality.

>>> arr
array([], shape=(0, 3), dtype=int64)

Then be sure to append along axis 0:

arr = np.append(arr, np.array([[1,2,3]]), axis=0)
arr = np.append(arr, np.array([[4,5,6]]), axis=0)

But, @jonrsharpe is right. In fact, if you're going to be appending in a loop, it would be much faster to append to a list as in your first example, then convert to a numpy array at the end, since you're really not using numpy as intended during the loop:

In [210]: %%timeit
.....: l = []
.....: for i in xrange(1000):
.....: l.append([3*i+1,3*i+2,3*i+3])
.....: l = np.asarray(l)
.....:
1000 loops, best of 3: 1.18 ms per loop

In [211]: %%timeit
.....: a = np.empty((0,3), int)
.....: for i in xrange(1000):
.....: a = np.append(a, 3*i+np.array([[1,2,3]]), 0)
.....:
100 loops, best of 3: 18.5 ms per loop

In [214]: np.allclose(a, l)
Out[214]: True

The numpythonic way to do it depends on your application, but it would be more like:

In [220]: timeit n = np.arange(1,3001).reshape(1000,3)
100000 loops, best of 3: 5.93 µs per loop

In [221]: np.allclose(a, n)
Out[221]: True

Python - How to create an empty numpy array and append to it, like lists

1. A Hack

You can create an empty array using the np.empty() function and specify the dimensions to be (0, 0) and then use np.append() to append items later.

>>> a = np.empty((0, 0))
>>> a
array([], shape=(0, 0), dtype=float64)
>>> b = np.append(a, [1, 2])
>>> b
array([1., 2.])

2. However...

The hack above is not advisable, use it with caution. Appending to lists has O(N) complexity, while appending to arrays has O(N^2) (besides different memory use). The proper way should be, then, to append to lists. Note that using list() on numpy arrays to transform them into lists is not correct, as you will get a list of numpy arrays. Instead, use the .tolist() method.

>>> a = np.array([[1, 2], [3, 4]])
>>>
>>> list(a)
[array([1, 2]), array([3, 4])]
>>>
>>> a.tolist()
[[1, 2], [3, 4]]

How do I create an empty array and then append to it in NumPy?

That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly.

Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row:

>>> import numpy as np

>>> a = np.zeros(shape=(3, 2))
>>> a
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])

>>> a[0] = [1, 2]
>>> a[1] = [3, 4]
>>> a[2] = [5, 6]

>>> a
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])

Numpy append to empty array

Look at what empty produces:

In [140]: x = np.empty((5,))
In [141]: x
Out[141]: array([0. , 0.25, 0.5 , 0.75, 1. ])

append makes a new array; it does not change x

In [142]: np.append(x, [1,2,3,4,5], axis=0)
Out[142]: array([0. , 0.25, 0.5 , 0.75, 1. , 1. , 2. , 3. , 4. , 5. ])
In [143]: x
Out[143]: array([0. , 0.25, 0.5 , 0.75, 1. ])

we have to assign it to a new variable:

In [144]: y = np.append(x, [1,2,3,4,5], axis=0)
In [145]: y
Out[145]: array([0. , 0.25, 0.5 , 0.75, 1. , 1. , 2. , 3. , 4. , 5. ])

Look at that y - those random values that were in x are also in y!

Contrast that with list

In [146]: alist = []
In [147]: alist
Out[147]: []
In [148]: alist.append([1,2,3,4,5])
In [149]: alist
Out[149]: [[1, 2, 3, 4, 5]]

The results are very different. Don't use this as a model for creating arrays.

If you need to build an array row by row, use the list append to collect the rows in one list, and then make the array from that.

In [150]: z = np.array(alist)
In [151]: z
Out[151]: array([[1, 2, 3, 4, 5]])

create an empty numpy array based and append existing array

Don't use np.append

np.append creates copies of data. You will find it inefficient. It should be avoided unless absolutely necessary.

Read into np.array and slice

Convert your tuple of single-item lists into a NumPy array and index directly:

D = np.array([[20, 23], [19, 23], [19, 22]])
index = ([0], [2])

E = D[np.array(index).ravel()]

Result:

array([[20, 23],
[19, 22]])

Unsuccessful append to an empty NumPy array

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 numpy

old = 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]

# Or
result = 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 np

a = 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.

Numpy append to an empty array

In your first case, you are creating an array called x that will containing one value, which is 0.

In your second case you are creating an empty array called x that will contain no values, but is still an array.

FIRST CASE

So when you append x = np.append(x,1), the value 1 get's appended to your array (which already contains 0) i.e. it now contains 0 and 1

SECOND CASE

Since you have no values in the empty array, when you append x=np.append(x,1) the value 1 get's appended and the length of x becomes 1 (i.e. it now contains only 1)

P.S. I believe you might have thought that calling x = np.array(0) with the 0 would make it an empty array, it doesn't!! In Python, 0 is still taken to be a number and is appended to the array.

how to append a numpy matrix into an empty numpy array

Option 1:
Reshape your initial All array to 3 columns so that the number of columns match h:

All=np.array([]).reshape((0,3))

for i in data:
h=i*Weights
All=np.concatenate((All,h))

All
#array([[ 8., 8., 8.],
# [ 8., 8., 8.],
# [ 8., 8., 8.],
# [ 12., 12., 12.],
# [ 12., 12., 12.],
# [ 12., 12., 12.]])

Option 2:
Use a if-else statement to handle initial empty array case:

All=np.array([])
for i in data:
h=i*Weights
if len(All) == 0:
All = h
else:
All=np.concatenate((All,h))

All
#array([[ 8, 8, 8],
# [ 8, 8, 8],
# [ 8, 8, 8],
# [12, 12, 12],
# [12, 12, 12],
# [12, 12, 12]])

Option 3:
Use itertools.product():

import itertools
np.array([i*j for i,j in itertools.product(data, Weights)])

#array([[ 8, 8, 8],
# [ 8, 8, 8],
# [ 8, 8, 8],
# [12, 12, 12],
# [12, 12, 12],
# [12, 12, 12]])


Related Topics



Leave a reply



Submit