I use to concatenate my numpy arrays of arbitrary shape to make my code cleaner, however, it seems pretty hard to me to iterate over it in a pythonesque way.
Lets consider a 4 dimension array x (thus len(x.shape) = 4
), and that the index I want to iterate is 2, the naive solution that I usually use is something like
y = np.array([my_operation(x[:, :, i, :])
for i in range(x.shape[2])])
I'm looking for something more readable, because it is annoying to have so many ":" and any changes in the dimensions of x would require rewrite a part of my code. Something magic like
y = np.array([my_operation(z) for z in magic_function(x, 2)])
Is there a numpy method that allows me to iterate over any arbitrary dimension of an array ?