if you know the number of columns before hand:
>>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = np.array([], dtype=np.int64).reshape(0,5)
>>> ys
array([], shape=(0, 5), dtype=int64)
>>> np.vstack([ys, xs])
array([[ 1., 2., 3., 4., 5.],
[ 10., 20., 30., 40., 50.]])
if not:
>>> ys = np.array([])
>>> ys = np.vstack([ys, xs]) if ys.size else xs
array([[ 1, 2, 3, 4, 5],
[10, 20, 30, 40, 50]])
Answer from behzad.nouri on Stack Overflowif you know the number of columns before hand:
>>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = np.array([], dtype=np.int64).reshape(0,5)
>>> ys
array([], shape=(0, 5), dtype=int64)
>>> np.vstack([ys, xs])
array([[ 1., 2., 3., 4., 5.],
[ 10., 20., 30., 40., 50.]])
if not:
>>> ys = np.array([])
>>> ys = np.vstack([ys, xs]) if ys.size else xs
array([[ 1, 2, 3, 4, 5],
[10, 20, 30, 40, 50]])
In Python, if possible to work with the individual vectors, to append you should use list.append()
>>> E = []
>>> B = np.array([1,2,3,4,5])
>>> C = np.array([10,20,30,40,50])
>>> E = E.append(B)
>>> E = E.append(C)
[array([1, 2, 3, 4, 5]), array([10, 20, 30, 40, 50])]
and then after all append operations are done, return to np.array thusly
>>> E = np.array(E)
array([[ 1, 2, 3, 4, 5],
[10, 20, 30, 40, 50]])
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
Here is my solution:
arr = []
arr.append([1,2,3])
arr.append([4,5,6])
np_arr = np.array(arr)
Your first use, with list append:
In [146]: alist=[]
In [147]: for i in range(4):
...: alist.append(np.arange(i,i+3))
...:
In [148]: alist
Out[148]: [array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4]), array([3, 4, 5])]
and make the array:
In [149]: np.array(alist)
Out[149]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
or since vstack is happy with a list of arrays:
In [150]: np.vstack(alist)
Out[150]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
You could use vstack in the loop:
In [151]: arr = np.zeros((0,3),int)
In [152]: for i in range(4):
...: arr = np.vstack((arr, np.arange(i,i+3)))
...:
In [153]: arr
Out[153]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
This has two problems:
it is slower; list append operates in-place simply adding a pointer to the list.
vstackmakes whole new array each time, with a full copy!it is harder to initialize, as you found out. You actually have to understand array shapes, and what
concatenatedoes when it combines 2 or more arrays. Here I started with a (0,3) array.
np.array([np.arange(i, i+3) for i in range(n)])
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.]])
A NumPy array is a very different data structure from a list and is designed to be used in different ways. Your use of hstack is potentially very inefficient... every time you call it, all the data in the existing array is copied into a new one. (The append function will have the same issue.) If you want to build up your matrix one column at a time, you might be best off to keep it in a list until it is finished, and only then convert it into an array.
e.g.
mylist = []
for item in data:
mylist.append(item)
mat = numpy.array(mylist)
item can be a list, an array or any iterable, as long
as each item has the same number of elements.
In this particular case (data is some iterable holding the matrix columns) you can simply use
mat = numpy.array(data)
(Also note that using list as a variable name is probably not good practice since it masks the built-in type by that name, which can lead to bugs.)
EDIT:
If for some reason you really do want to create an empty array, you can just use numpy.array([]), but this is rarely useful!
If by empty array, you mean an array with 0 dimensions, you can simply convert to a NumPy array an empty list:
import numpy as np
np.array([])
# array([], dtype=float64)
or use any of the NumPy's initialization functions with a (0,) or 0 shape, e.g.:
np.zeros((0,))
# array([], dtype=float64)
np.ones((0,))
# array([], dtype=float64)
np.empty((0,))
# array([], dtype=float64)
np.full((0,), 0.0)
# array([], dtype=float64)
etc.
If by empty you mean an array of given size but not initialized (i.e. you only ask the OS for the required memory), you can use
np.empty() specifying the size you need, e.g.:
np.empty((2, 3))
# array([[1.46643506e-316, 0.00000000e+000, 0.00000000e+000],
# [0.00000000e+000, 0.00000000e+000, 0.00000000e+000]])
There are two ways an empty NumPy array can be created: numpy.zeros and numpy.empty.
The syntax for using numpy.zeros and numpy.empty is shown below:
numpy.zeros(shape, dtype=float, order='C')
numpy.empty(shape, dtype=float, order='C')
# Shape -> Shape of the new array, e.g., (2, 3) or 2.
# dtype -> The desired data-type for the array,e.g., numpy.int8. Default is numpy.float64. This parameter is optional.
# order -> Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory. This parameter is optional.
how to use numpy.zeros to create an empty NumPy array:
import numpy as np
myArr = np.zeros((2,3))
print(myArr)
how to use numpy.empty to create an empty NumPy array:
import numpy as np
myArr = np.empty((2,3))
print(myArr)
numpy.empty, unlike numpy.zeros, does not set the array values to zero and, therefore, may be marginally faster. On the other hand, it requires the user to manually set all the values in the array and should be used with caution.
You need to append the two numbers inside another list because of the second dim.
arr = np.empty((0,2),int)
arr = np.append(arr, [[0, 1]], axis=0)
arr = np.append(arr, [[1, 1]], axis=0)
arr = np.append(arr, [[5, 1]], axis=0)
arr should now be:
array([[0, 1],
[1, 1],
[5, 1]])
You can use deque and then convert it to numpy array
from collections import deque
import numpy as np
lists = deque()
lists.append([0,1])
lists.append([1,1])
lists.append([5,1])
results = np.array(lists)
results
Output
array([[0, 1],
[1, 1],
[5, 1]])