There is something that I would very appreciate, it is the ability to name the dimensions in an array in python. For example I have a numpy array with 3 dimensions and I will regularly have to sum it along a specific dimensions.
So I can do with a ndarray a
:
sum(a, axis=2)
if my relevant dimension is the last one, but I want to make it "position independent", i.e. a user can provide any array, as long as he specifies "this dimension is "DI" " (example, for "Dimension of Interest"). So basically I would like to be able to write:
sum(a, axis="DI")
Close to NETCDF, but I don't want to implement a whole netcdf capability.
@M456's idea is clever, but if you have the same naming scheme for several arrays, I think the simpler solution would be just to use a dictionary:
axes = { 'DA': 0, 'DB':1 }
a.sum(axes['DA'])
or even just variables:
DA, DB, DC = range(3)
a.sum(DA)
If it should be your last (or penultimate, etc) axis, just use -1
(or -2
, etc.):
a.shape
#(2,3,4)
np.all(a.sum(2) == a.sum(-1))
#True
np.all(a.sum(0) == a.sum(-3))
#True