numpy.zerosReturn a new array of given shape and type, filled with zeros.
or
numpy.onesReturn a new array of given shape and type, filled with ones.
or
numpy.emptyReturn a new array of given shape and type, without initializing entries.
However, the mentality in which we construct an array by appending elements to a list is not much used in numpy, because it's less efficient (numpy datatypes are much closer to the underlying C arrays). Instead, you should preallocate the array to the size that you need it to be, and then fill in the rows. You can use numpy.append if you must, though.
python - initialize a numpy array - Stack Overflow
python - How can I create a numpy array with shape (0, 2)? - Stack Overflow
what's the difference between a numpy array with shape (x,) and (x, 1)?
How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
numpy.zerosReturn a new array of given shape and type, filled with zeros.
or
numpy.onesReturn a new array of given shape and type, filled with ones.
or
numpy.emptyReturn a new array of given shape and type, without initializing entries.
However, the mentality in which we construct an array by appending elements to a list is not much used in numpy, because it's less efficient (numpy datatypes are much closer to the underlying C arrays). Instead, you should preallocate the array to the size that you need it to be, and then fill in the rows. You can use numpy.append if you must, though.
The way I usually do that is by creating a regular list, then append my stuff into it, and finally transform the list to a numpy array as follows :
Use with AIimport numpy as np
big_array = [] # empty regular list
for i in range(5):
arr = i*np.ones((2,4)) # for instance
big_array.append(arr)
big_np_array = np.array(big_array) # transformed to a numpy array
of course your final object takes twice the space in the memory at the creation step, but appending on python list is very fast, and creation using np.array() also.
Use a function such as np.zeros. No need to worry about the value it generates since there are no values in a 0 x 2 array:
arr = np.zeros((0, 2))
arr.shape
(0, 2)
It can be done by np.empty((0,2)) or np.zeros((0,2)).
np.empty(), unlike np.zeros(), does not set the array values to zero and it is slightly faster.
Extra information:
In your code np.array([]).shape outputs a rank-1 array which is equal to its transpose.