Skip to content Skip to sidebar Skip to footer

Numpy Array Multiple Mask

Trying to slice and average a numpy array multiple times, based on an integer mask array: i.e. import numpy as np data = np.arange(11) mask = np.array([0, 1, 1, 1, 0, 2, 2, 3, 3,

Solution 1:

One approach using np.bincount -

np.bincount(mask, data)/np.bincount(mask)

Another one with np.unique for a generic case when the elements in mask aren't necessarily sequential starting from 0 -

_,ids, count = np.unique(mask, return_inverse=1, return_counts=1)
out = np.bincount(ids, data)/count

Post a Comment for "Numpy Array Multiple Mask"