API reference
Reductions
Each one exists as a function (np.sum(a, opts)) and as a method (a.sum(opts)). The result is always an NDArray (0-d when every axis is reduced); use .item() to get a JS number.
| Name | Summary |
|---|---|
np.sum | Sum or product of elements. |
np.max | Largest or smallest element. |
np.mean | Arithmetic mean. |
np.std | Standard deviation and variance. |
np.argmax | int64 index of the first maximum/minimum. |
np.sum
#np.sum(a, [options]) · np.prod(a, [options])
Sum or product of elements. Small integer types accumulate in int64/uint64, as in NumPy.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | number[] | null- Axis or axes to reduce. Omitted reduces all axes.
[options.keepdims]boolean- Keep reduced axes with length 1. Default
false. [options.dtype]DTypeLike- Accumulator/result dtype.
[options.initial]number- Starting value.
Returns
NDArray
Example
TypeScript
const a = np.array([[1, 2, 3], [4, 5, 6]]);
np.sum(a).item(); // => 21
np.sum(a, { axis: 0 }); // => [5, 7, 9]
a.sum({ axis: 1, keepdims: true }); // => [[6], [15]]
np.prod([1, 2, 3, 4]).item(); // => 24TypeScript declaration
np.sum(a: ArrayLike, opts?: ReduceOptions | undefined): NDArray
np.prod(a: ArrayLike, opts?: ReduceOptions | undefined): NDArraynp.max
#np.max(a, [options]) · np.min(a, [options])
Largest or smallest element. amax and amin are aliases. Empty reductions without initial throw ValueError.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | number[] | null- Axis or axes to reduce. Omitted reduces all axes.
[options.keepdims]boolean- Keep reduced axes with length 1. Default
false. [options.initial]number- Value included in the reduction.
Returns
NDArray
Example
TypeScript
const a = np.array([[1, 2, 3], [4, 5, 6]]);
a.max({ axis: 1 }); // => [3, 6]
np.min(a).item(); // => 1
np.amax([-1, 5]).item(); // => 5TypeScript declaration
np.max(a: ArrayLike, opts?: Omit<ReduceOptions, "dtype"> | undefined): NDArray
np.min(a: ArrayLike, opts?: Omit<ReduceOptions, "dtype"> | undefined): NDArraynp.mean
#np.mean(a, [options])
Arithmetic mean. Integer inputs produce float64.
NumPynumpy.mean
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | number[] | null- Axis or axes to reduce. Omitted reduces all axes.
[options.keepdims]boolean- Keep reduced axes with length 1. Default
false. [options.dtype]DTypeLike- Accumulator/result dtype.
Returns
NDArray
Example
TypeScript
np.mean([[1, 2], [3, 4]], { axis: 0 }); // => [2, 3]TypeScript declaration
np.mean(a: ArrayLike, opts?: Omit<ReduceOptions, "initial"> | undefined): NDArraynp.std
#np.std(a, [options]) · np.var(a, [options])
Standard deviation and variance. ddof is the delta degrees of freedom, so ddof: 1 gives the sample estimate. np.var is also exported by name as variance.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | number[] | null- Axis or axes to reduce. Omitted reduces all axes.
[options.keepdims]boolean- Keep reduced axes with length 1. Default
false. [options.ddof]number- Divisor is
N - ddof. Default0.
Returns
NDArray
Example
TypeScript
np.var([1, 2, 3, 4]).item(); // => 1.25
np.std([2, 4, 4, 4, 5, 5, 7, 9]).item(); // => 2
np.var([1, 2, 3, 4], { ddof: 1 }).item(); // => 1.6666666666666667TypeScript declaration
np.std(a: ArrayLike, opts?: VarOptions | undefined): NDArray
np.var(a: ArrayLike, opts?: VarOptions | undefined): NDArraynp.argmax
#np.argmax(a, [options]) · np.argmin(a, [options])
int64 index of the first maximum/minimum. Without axis, the index is into the flattened array.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | null- A single axis. Default: flattened.
[options.keepdims]boolean- Keep the reduced axis.
Returns
NDArray
Example
TypeScript
const a = np.array([[1, 9, 3], [7, 2, 8]]);
np.argmax(a).item(); // => 1
np.argmin(a, { axis: 1 }); // => [0, 1]TypeScript declaration
np.argmax(a: ArrayLike, opts?: ArgReduceOptions | undefined): NDArray
np.argmin(a: ArrayLike, opts?: ArgReduceOptions | undefined): NDArray