--NumPy allows you to define zeros of size 0 like np.zeros ((0, 4))
--With this, you can also define an empty list and add it with append () with array.
What I often do when creating lists in Python
a = []
for i in range(10):
a.append(1)
Someone like
If you try to do the same with a NumPy Array, you'll end up using numpy.vstack () or numpy.hstack () → Since the number of rows (or columns) of the two Arrays to be attached must match, we first defined an Array with shape = (1, 4).
Bad person
import numpy as np
a = np.zeros((1, 4))
for i in range(10):
ones = np.ones((1, 4))
a = a.vstack((a, ones))
a = a[1:, :] #Only the first defined part is excluded
I felt uncomfortable with the operation of defining an Array of shape = (1, 4) first and then removing it, so I had to define zeros of size 0 as a trial.
Easy to see
import numpy as np
a = np.zeros((0, 4)) #Zero size zeros
for i in range(10):
ones = np.ones((1, 4))
a = a.vstack((a, ones))
Recommended Posts