I cannot stack numpy arrays over new dimension because of the following problem:
>>> a = np.zeros(shape=(100,100))
>>> b = np.zeros(shape=(100,100))
>>> c = np.stack((a, b))
>>> c.shape
(2, 100, 100)
>>> d = np.zeros(shape=(100,100))
>>> c = np.stack((c, d))
Traceback (most recent call last):
File "/lib/python3.7/site-packages/numpy/core/shape_base.py", line 426, in stack
raise ValueError('all input arrays must have the same shape')
ValueError: all input arrays must have the same shape
The way I intend to use it in a loop is:
final = None
for next_mat in mats:
final = next_mat if final is None else np.stack((final, next_mat))
How do I achieve it? Thank you!
np.stackadds a new dimension each time you use it. And yes, it is very picky about the shape all all the input arrays.