How do you append an array to an array in numpy?
My code
na = np.append(na , b, axis = 0 )
where
na = np.array([], dtype=float)
b = [1,2,3,4]
output: [1,2,3,4]
Want : [[1,2,3,4]]
How do you append an array to an array in numpy?
My code
na = np.append(na , b, axis = 0 )
where
na = np.array([], dtype=float)
b = [1,2,3,4]
output: [1,2,3,4]
Want : [[1,2,3,4]]
There's one basic function for joining one array to another, np.concatenate, and set that make certain types of concatenation a bit easier (but not faster), vstack, hstack, column_stack, stack, append. Read their docs.
A key point is that enough of the dimensions have to match. Your na has shape (0,). The only thing that matches in shape is itself, and the result is itself.
Here's one way of producing your target from your b (which is effectly a (4,) array (check np.array(b).shape):
In [460]: na=np.zeros((0,4),int)
In [461]: np.vstack((na, [1,2,3,4]))
Out[461]: array([[1, 2, 3, 4]])
The result is (1,4) a array. You could have produced that without concatenation
In [466]: np.atleast_2d(b)
Out[466]: array([[1, 2, 3, 4]])
Study shape and dimensions some more, and play with the basic concatenate. It's tempting to jump into using append or one the stack without understanding the basic issues.