API reference
Reductions with where= / out=
All seven scalar reductions (sum, prod, min, max, mean, var, std) accept where (a boolean mask broadcast to a) and out (a pre-allocated result array). Only elements where the mask is true contribute; masked-out positions use the reduction identity (0 for sum, 1 for prod; for min/max you must supply initial). mean/var/std divide by the count of true positions.
| Name | Summary |
|---|---|
sum | where is a bool ArrayLike broadcast to a. |
mean | Mean of the unmasked elements; divisor is the count of true positions. |
var | Variance / standard deviation over unmasked elements. |
min | min/max with a mask require initial (the identity / seed value), matching NumPy's requirement. |
out= | Write the result into a pre-allocated NDArray. |
sum (where=)
#np.sum(a, { where, out? })
where is a bool ArrayLike broadcast to a. Masked-out positions contribute 0.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. options.whereArrayLike- Boolean mask;
truepositions are summed. [options.out]NDArray- Pre-allocated output (exact result shape).
Returns
NDArray
Example
const a = np.array([1, 2, 3, 4, 5], { dtype: "float64" });
const mask = np.array([true, false, true, false, true]);
np.sum(a, { where: mask }).item(); // => 9TypeScript declaration
np.sum(a: ArrayLike, opts?: ReduceOptions | undefined): NDArraymean (where=)
#np.mean(a, { where, out? })
Mean of the unmasked elements; divisor is the count of true positions.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. options.whereArrayLike- Boolean mask.
Returns
NDArray
Example
const a = np.array([1, 2, 3, 4, 5], { dtype: "float64" });
const mask = np.array([true, false, true, false, true]);
np.mean(a, { where: mask }).item(); // => 3TypeScript declaration
np.mean(a: ArrayLike, opts?: Omit<ReduceOptions, "initial"> | undefined): NDArrayvar / std (where=)
#np.var(a, { where, ddof? }) · np.std(a, { where, ddof? })
Variance / standard deviation over unmasked elements.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. options.whereArrayLike- Boolean mask.
[options.ddof]number- Delta degrees of freedom (default 0).
Returns
NDArray
Example
const a = np.array([1, 2, 3, 4, 5], { dtype: "float64" });
const mask = np.array([true, false, true, false, true]);
np.var(a, { where: mask }).item(); // => 2.6666666666666665
np.std(a, { where: mask }).item(); // => 1.632993161855452TypeScript declaration
np.var(a: ArrayLike, opts?: VarOptions | undefined): NDArray
np.std(a: ArrayLike, opts?: VarOptions | undefined): NDArraymin / max (where= + initial=)
#np.min(a, { where, initial }) · np.max(a, { where, initial })
min/max with a mask require initial (the identity / seed value), matching NumPy's requirement.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. options.whereArrayLike- Boolean mask.
options.initialnumber- Required seed value.
Returns
NDArray
Example
const a = np.array([1, 2, 3, 4], { dtype: "float64" });
const mask = np.array([false, true, false, true]);
np.min(a, { where: mask, initial: 1e10 }).item(); // => 2
np.max(a, { where: mask, initial: -1e10 }).item(); // => 4TypeScript declaration
np.min(a: ArrayLike, opts?: Omit<ReduceOptions, "dtype"> | undefined): NDArray
np.max(a: ArrayLike, opts?: Omit<ReduceOptions, "dtype"> | undefined): NDArrayout= parameter
#np.sum/prod/min/max/mean/var/std(a, { out })
Write the result into a pre-allocated NDArray. out.shape must exactly match the result shape. The same array is returned.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. options.outNDArray- Pre-allocated output (exact result shape).
Returns
NDArray (same object as out)
Example
const a = np.array([1, 2, 3, 4, 5], { dtype: "float64" });
const out = np.zeros([]);
np.sum(a, { out });
out.item(); // => 15TypeScript declaration
np.sum(a: ArrayLike, opts?: ReduceOptions | undefined): NDArray