I think I'll forget it three months later, so make a note.
I get angry when I try to np.r \ _ on an empty array with numpy.
arr = np.array([])
additional = np.array([[1, 2, 3], [4, 5, 6]])
arr = np.r_[arr, additional]
print(arr)
>>>
ValueError: all the input arrays must have same number of dimensions
However, I don't think this method is Pythonic.
arr = np.zeros([1, 3])
additional = np.array([[1, 2, 3], [4, 5, 6]])
arr = np.r_[arr, additional][1:]
print(arr)
>>>
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
This can be solved with numpy.empty.
arr = np.empty([0, 3])
additional = np.array([[1, 2, 3], [4, 5, 6]])
arr = np.r_[arr, additional]
print(arr)
>>>
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
Use this method when adding arrays little by little in a for loop.
arr = np.empty([0, 3])
for i in range(3):
additional = np.ones([1, 3]) * i
arr = np.r_[arr, additional]
print(arr)
>>>
[[ 0. 0. 0.]
[ 1. 1. 1.]
[ 2. 2. 2.]]
Recommended Posts