flopscope.numpy.putmask
fnp.putmask(a, mask, values, *args, **kwargs)[flopscope source]
Changes elements of an array based on conditional and input values.
Adapted from NumPy docs np.putmask
Cost
4×per-operation
Flopscope Context
Change elements of array based on condition and input values. Cost: numel(input).
Sets a.flat[n] = values[n] for each n where mask.flat[n]==True.
If values is not the same size as a and mask then it will repeat.
This gives behavior different from a[mask] = values.
Parameters
- a:ndarray
Target array.
- mask:array_like
Boolean mask array. It has to be the same shape as
a.- values:array_like
Values to put into
awheremaskis True. Ifvaluesis smaller thanait will be repeated.
See also
Examples
>>> import flopscope.numpy as fnp
>>> x = flops.arange(6).reshape(2, 3)
>>> flops.putmask(x, x>2, x**2)
>>> x
array([[ 0, 1, 2],
[ 9, 16, 25]])If values is smaller than a it is repeated:
>>> x = flops.arange(5)
>>> flops.putmask(x, x>1, [-33, -44])
>>> x
array([ 0, 1, -33, -44, -33])