I am trying to fill an empty(not np.empty!) array with values using append but I am gettin error:
My code is as follows:
import numpy as np
result=np.asarray([np.asarray([]),np.asarray([])])
result[0]=np.append([result[0]],[1,2])
And I am getting:
ValueError: could not broadcast input array from shape (2) into shape (0)
Here's the result of running your code in Ipython. Note that
result
is a(2,0)
array, 2 rows, 0 columns, 0 elements. Theappend
produces a(2,)
array.result[0]
is(0,)
array. Your error message has to do with trying to assign that 2 item array into a size 0 slot. Sinceresult
isdtype=float64
, only scalars can be assigned to its elements.np.array
is not a Python list. All elements of an array are the same type (as specified by thedtype
). Notice also thatresult
is not an array of arrays.Result could also have been built as
while
the same is not true for result.
np.zeros((2,0))
also produces yourresult
.Actually there's another quirk to
result
.does not change the values of
result
. It accepts the assignment, but since it has 0 columns, there is no place to put the1
. This assignment would work in result was created asnp.zeros((2,1))
. But that still can't accept a list.But if
result
has 2 columns, then you can assign a 2 element list to one of its rows.What exactly do you want
result
to look like after theappend
operation?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:
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:
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:
Concatenate rows:
Concatenate columns:
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:
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?
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:I think you might be able to achieve your goal by doing something like:
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:
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 ofa
andb
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
always copies the array before appending the new values. Your code is equivalent to the following:Perhaps you mean to do this?
SO thread 'Multiply two arrays element wise, where one of the arrays has arrays as elements' has an example of constructing an array from arrays. If the subarrays are the same size, numpy makes a 2d array. But if they differ in length, it makes an array with
dtype=object
, and the subarrays retain their identity.Following that, you could do something like this:
However, I don't offhand see what advantages this has over a pure Python list or lists. It does not work like a 2d array. For example I have to use
result[0][1]
, notresult[0,1]
. If the subarrays are all the same length, I have to usenp.array(result.tolist())
to produce a 2d array.