python - NumPy Vectorize a function, unknown shape -
i have function take numpy array parameter, example:
def f(arr): return arr.sum() and want create numpy array each vec in a, if a.shape = (14,12,7), function myfunc(a).shape = (14,12)
i.e. myfunc(a)[x, y] = f(a[x, y])
note len(a.shape) not specified.
you can apply sum along last axis:
a.sum(axis=-1) for example:
in [1]: np.ones((14,12,7)).sum(axis=-1).shape out[1]: (14, 12) if have generic function can use apply_along_axis:
np.apply_along_axis(sum, -1, a)
Comments
Post a Comment