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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -