Rearranging axes in numpy?
问题 I have an ndarray such as >>> arr = np.random.rand(10, 20, 30, 40) >>> arr.shape (10, 20, 30, 40) whose axes I would like to swap around into some arbitrary order such as >>> rearranged_arr = np.swapaxes(np.swapaxes(arr, 1,3), 0,1) >>> rearranged_arr.shape (40, 10, 30, 20) Is there a function which achieves this without having to chain together a bunch of np.swapaxes ? 回答1: There are two options: np.moveaxis and np.transpose. np.moveaxis(a, sources, destinations) docs This function can be