API reference
Shape manipulation
All of these return views (no data copy) unless noted. Each one exists as a free function (np.transpose(a)) and, for most, as a method (a.transpose()).
| Name | Summary |
|---|---|
np.reshape | Function form of a.reshape(shape). |
np.transpose | Permutes the axes. |
np.squeeze | Removes length-1 axes: all of them, or only the ones given. |
np.expandDims | Inserts length-1 axes at the given positions. |
np.swapAxes | Swaps two axes. |
np.moveAxis | Moves axes to new positions; the other axes keep their relative order. |
np.ravel | Flattens to 1-D. |
np.broadcastTo | Read-only view of a broadcast to shape, without copying. |
np.broadcastShapes | The shape that the given shapes broadcast to. |
np.seterr | Sets how floating-point errors are handled: "ignore", "warn" (a Node RuntimeWarning), "raise" (FloatingPointError) or "print". |
np.geterr | The current floating-point error settings. |
np.errstate | Runs fn synchronously with the given error settings and restores the previous ones afterwards. |
np.concatenate | Joins arrays along an existing axis (default 0). |
np.stack | Joins same-shaped arrays along a new axis. |
np.vstack | Stacks inputs (made at least 2-d) along axis 0. |
np.hstack | Joins along axis 1, or axis 0 for 1-d inputs. |
np.dstack | Joins along axis 2 after making each input at least 3-d. |
np.columnStack | Stacks 1-d arrays as columns of a 2-d array (2-d inputs are joined as they are). |
np.block | Assembles an array from nested lists of blocks: the innermost lists join along the last axis, the next level along the one before, and so on. |
np.unstack | Splits an array into a list of views along axis (default 0), removing that axis. |
np.split | Splits into equal sections (must divide the axis) or at the given indices. |
np.atleast1d | Views with at least 1 (atleast1d), 2 (atleast2d, (N) → (1, N)) or 3 (atleast3d, (N) → (1, N, 1), (M, N) → (M, N, 1)) dimensions. |
np.tile | Repeats the whole array reps times along each axis; reps shorter than a.ndim is padded with 1s on the left, longer prepends new axes. |
np.repeat | Repeats each element repeats times (an integer, or one count per element along axis). |
np.pad | Pads an array. |
np.append | Appends values to arr (both flattened when axis is omitted); a new array. |
np.insert | Inserts values before index/indices obj (an integer, list, boolean mask or slice object {start, stop, step}). |
np.delete | Removes the entries at obj (integer, list, boolean mask or slice object) along axis, flattening first when axis is omitted. |
np.resize | np.resize returns a new array filled by repeating a's data. |
np.trimZeros | Trims leading ("f") and/or trailing ("b") zeros (default "fb"). |
np.flip | Reverses element order along axis (an integer or list; all axes when omitted). |
np.roll | Shifts elements cyclically. |
np.rot90 | Rotates by 90° k times (default 1) in the plane of axes (default [0, 1]), from the first axis towards the second. |
np.rollaxis | Moves axis so that it lies before position start (default 0). |
np.copyto | Copies src (broadcast to dst's shape) into dst in place. |
np.broadcastArrays | Broadcasts the inputs against each other and returns views of the common shape (inputs already of that shape are returned as is). |
np.asanyarray | asanyarray is asarray (numera has no array subclasses). |
np.require | Returns a as an array of dtype that satisfies the requirement flags, copying only if needed: C/C_CONTIGUOUS/CONTIGUOUS, F/F_CONTIGUOUS/FORTRAN, A/ALIGNED, W/WRITEABLE, O/OWNDATA, E/ENSUREARRAY, as a string of letters or a list. |
np.shape | Metadata of any array-like: shape, number of elements (or the length of axis), number of dimensions. |
np.applyAlongAxis | Calls the JS function func1d(lane, ...args) on every 1-d lane of arr along axis. |
np.applyOverAxes | Applies func(val, axis) for each axis in turn; a result with one dimension fewer gets the axis back as length 1 (like keepdims). |
np.reshape
#np.reshape(a, shape)
Function form of a.reshape(shape).
Parameters
aNDArray- Input array.
shapenumber | number[]- New shape; one entry may be
-1.
Returns
NDArray
Example
np.reshape(np.arange(4), [2, 2]); // => [[0, 1], [2, 3]]TypeScript declaration
np.reshape(a: NDArray, shape: number | Shape, opts?: OrderOptions | undefined): NDArraynp.transpose
#np.transpose(a, [axes])
Permutes the axes. By default the order is reversed.
Parameters
aNDArray- Input array.
[axes]number[]- New axis order.
Returns
NDArray
Example
np.transpose(np.zeros([2, 3, 4])).shape; // => [4, 3, 2]
np.transpose(np.zeros([2, 3, 4]), [0, 2, 1]).shape; // => [2, 4, 3]TypeScript declaration
np.transpose(a: NDArray, axes?: readonly number[] | undefined): NDArraynp.squeeze
#np.squeeze(a, [axis])
Removes length-1 axes: all of them, or only the ones given.
Parameters
aNDArray- Input array.
[axis]number | number[]- Axes to remove (each must have length 1).
Returns
NDArray
Example
np.squeeze(np.zeros([1, 3, 1])).shape; // => [3]
np.squeeze(np.zeros([1, 3, 1]), 0).shape; // => [3, 1]TypeScript declaration
np.squeeze(a: NDArray, axis?: number | readonly number[] | undefined): NDArraynp.expandDims
#np.expandDims(a, axis)
Inserts length-1 axes at the given positions.
Parameters
aNDArray- Input array.
axisnumber | number[]- Positions of the new axes in the result.
Returns
NDArray
Example
np.expandDims(np.zeros([3]), 0).shape; // => [1, 3]
np.expandDims(np.zeros([3]), [0, 2]).shape; // => [1, 3, 1]TypeScript declaration
np.expandDims(a: NDArray, axis: number | readonly number[]): NDArraynp.swapAxes
#np.swapAxes(a, axis1, axis2)
Swaps two axes.
Parameters
aNDArray- Input array.
axis1, axis2number- The axes to swap.
Returns
NDArray
Example
np.swapAxes(np.zeros([2, 3, 4]), 0, 2).shape; // => [4, 3, 2]TypeScript declaration
np.swapAxes(a: NDArray, axis1: number, axis2: number): NDArraynp.moveAxis
#np.moveAxis(a, source, destination)
Moves axes to new positions; the other axes keep their relative order.
Parameters
aNDArray- Input array.
sourcenumber | number[]- Original positions.
destinationnumber | number[]- New positions.
Returns
NDArray
Example
np.moveAxis(np.zeros([2, 3, 4]), 0, -1).shape; // => [3, 4, 2]TypeScript declaration
np.moveAxis(a: NDArray, source: number | readonly number[], destination: number | readonly number[]): NDArraynp.ravel
#np.ravel(a)
Flattens to 1-D. Returns a view when possible, otherwise a copy.
Parameters
aNDArray- Input array.
Returns
NDArray
Example
np.ravel(np.array([[1, 2], [3, 4]])); // => [1, 2, 3, 4]TypeScript declaration
np.ravel(a: NDArray, opts?: OrderOptions | undefined): NDArraynp.broadcastTo
#np.broadcastTo(a, shape)
Read-only view of a broadcast to shape, without copying.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. shapenumber | number[]- Target shape.
Returns
NDArray
Example
np.broadcastTo([1, 2], [2, 2]); // => [[1, 2], [1, 2]]TypeScript declaration
np.broadcastTo(a: ArrayLike, shape: number | Shape): NDArraynp.broadcastShapes
#np.broadcastShapes(...shapes)
The shape that the given shapes broadcast to. Throws BroadcastError if they are incompatible.
Parameters
...shapesnumber | number[]- Input shapes.
Returns
number[]
Example
np.broadcastShapes([2, 1], [3]); // => [2, 3]TypeScript declaration
np.broadcastShapes(...shapes: (number | Shape)[]): number[]np.seterr
#np.seterr({ all?, divide?, over?, under?, invalid? })
Sets how floating-point errors are handled: "ignore", "warn" (a Node RuntimeWarning), "raise" (FloatingPointError) or "print". Returns the previous settings.
Parameters
settingsobject- Modes per category;
allsets every category.
Returns
object
Example
const old = np.seterr({ all: "ignore" });
np.seterr(old).divide; // => "ignore"TypeScript declaration
np.seterr(settings?: ErrSettings | undefined): ErrStatenp.geterr
#np.geterr()
The current floating-point error settings.
Returns
object
Example
np.geterr().under; // => "ignore"TypeScript declaration
np.geterr(): ErrStatenp.errstate
#np.errstate(settings, fn)
Runs fn synchronously with the given error settings and restores the previous ones afterwards.
Parameters
settingsobject- As for
seterr. fn() => T- Function to run.
Returns
T
Example
np.errstate({ divide: "ignore" }, () => np.divide([1], [0]).toArray()[0] === Infinity); // => trueTypeScript declaration
np.errstate<T>(settings: ErrSettings, fn: () => T): Tnp.concatenate
#np.concatenate(arrays, [axis | {axis, dtype, casting, out}])
Joins arrays along an existing axis (default 0). axis: null flattens the inputs first. concat is the same function (array API name). casting defaults to "same_kind"; out and dtype cannot be combined.
Parameters
arraysArrayLike[]- Arrays with the same shape except along
axis. [axis]number | null | object- Join axis, or an options object.
Returns
NDArray (or `out`)
Example
np.concatenate([[1, 2], [3]]); // => [1, 2, 3]
np.concatenate([[[1], [2]], [[3], [4]]], 1); // => [[1, 3], [2, 4]]
np.concat([[[1, 2]], [3]], { axis: null }); // => [1, 2, 3]
np.concatenate([[1.7], [2]], { dtype: "int8", casting: "unsafe" }); // => [1, 2]TypeScript declaration
np.concatenate(arrays: Sequence, axis?: number | ConcatenateOptions | null | undefined, options?: JoinOptions | undefined): NDArraynp.stack
#np.stack(arrays, [axis | {axis, dtype, casting, out}])
Joins same-shaped arrays along a new axis.
Parameters
arraysArrayLike[]- Arrays of identical shape.
Returns
NDArray
Example
np.stack([[1, 2], [3, 4]], 1); // => [[1, 3], [2, 4]]TypeScript declaration
np.stack(arrays: Sequence, axis?: number | StackOptions | undefined, options?: JoinOptions | undefined): NDArraynp.vstack
#np.vstack(arrays, [{dtype, casting}])
Stacks inputs (made at least 2-d) along axis 0.
Returns
NDArray
Example
np.vstack([[1, 2], [3, 4]]); // => [[1, 2], [3, 4]]TypeScript declaration
np.vstack(arrays: Sequence, options?: VHStackOptions | undefined): NDArraynp.hstack
#np.hstack(arrays, [{dtype, casting}])
Joins along axis 1, or axis 0 for 1-d inputs.
Returns
NDArray
Example
np.hstack([[1, 2], [3]]); // => [1, 2, 3]TypeScript declaration
np.hstack(arrays: Sequence, options?: VHStackOptions | undefined): NDArraynp.dstack
#np.dstack(arrays)
Joins along axis 2 after making each input at least 3-d.
Returns
NDArray
Example
np.dstack([[1, 2], [3, 4]]); // => [[[1, 3], [2, 4]]]TypeScript declaration
np.dstack(arrays: Sequence): NDArraynp.columnStack
#np.columnStack(arrays)
Stacks 1-d arrays as columns of a 2-d array (2-d inputs are joined as they are).
Returns
NDArray
Example
np.columnStack([[1, 2], [3, 4]]); // => [[1, 3], [2, 4]]TypeScript declaration
np.columnStack(arrays: Sequence): NDArraynp.block
#np.block(arrays)
Assembles an array from nested lists of blocks: the innermost lists join along the last axis, the next level along the one before, and so on.
Parameters
arraysnested (NDArray | number)[]- Nested lists of arrays or scalars.
Returns
NDArray
Example
np.block([[np.ones([1, 2]), np.zeros([1, 1])], [np.zeros([1, 3])]]); // => [[1, 1, 0], [0, 0, 0]]TypeScript declaration
np.block(arrays: BlockArg): NDArraynp.unstack
#np.unstack(x, [axis])
Splits an array into a list of views along axis (default 0), removing that axis.
Returns
NDArray[]
Example
np.unstack(np.array([[1, 2], [3, 4]]), 1).map((v) => v.toArray()); // => [[1, 3], [2, 4]]TypeScript declaration
np.unstack(x: ArrayLike, axis?: number | { axis?: number | undefined; } | undefined): NDArray[]np.split
#np.split(a, sectionsOrIndices, [axis])
Splits into equal sections (must divide the axis) or at the given indices. Returns views. arraySplit allows unequal sections; hsplit, vsplit and dsplit split along axis 1 (0 for 1-d), 0 and 2.
Parameters
aArrayLike- Array to split.
sectionsOrIndicesnumber | number[]- Number of sections, or split points.
Returns
NDArray[]
Example
np.split(np.arange(6), 3).map((v) => v.toArray()); // => [[0, 1], [2, 3], [4, 5]]
np.split(np.arange(5), [2]).map((v) => v.toArray()); // => [[0, 1], [2, 3, 4]]
np.arraySplit(np.arange(5), 2).map((v) => v.toArray()); // => [[0, 1, 2], [3, 4]]
np.hsplit(np.arange(4).reshape(2, 2), 2)[1].toArray(); // => [[1], [3]]
np.vsplit(np.arange(4).reshape(2, 2), 2)[1].toArray(); // => [[2, 3]]
np.dsplit(np.zeros([1, 1, 4]), 2)[0].shape; // => [1, 1, 2]TypeScript declaration
np.split(a: ArrayLike, indicesOrSections: number | NDArray | readonly number[], axis?: number | undefined): NDArray[]np.atleast1d
#np.atleast1d(...arys)
Views with at least 1 (atleast1d), 2 (atleast2d, (N) → (1, N)) or 3 (atleast3d, (N) → (1, N, 1), (M, N) → (M, N, 1)) dimensions. One argument returns one array, several return a list.
Returns
NDArray | NDArray[]
Example
np.atleast1d(5).shape; // => [1]
np.atleast2d([1, 2]).shape; // => [1, 2]
np.atleast3d([1, 2]).shape; // => [1, 2, 1]
np.atleast1d(1, [2, 3]).length; // => 2TypeScript declaration
np.atleast1d(a: ArrayLike): NDArray
np.atleast1d(...arys: ArrayLike[]): NDArray | NDArray[]np.tile
#np.tile(a, reps)
Repeats the whole array reps times along each axis; reps shorter than a.ndim is padded with 1s on the left, longer prepends new axes. Always returns a copy.
Parameters
repsnumber | number[]- Repetitions per axis.
Returns
NDArray
Example
np.tile([1, 2], 2); // => [1, 2, 1, 2]
np.tile([1, 2], [2, 1]); // => [[1, 2], [1, 2]]TypeScript declaration
np.tile(a: ArrayLike, reps: IntList): NDArraynp.repeat
#np.repeat(a, repeats, [axis]) / a.repeat(repeats, [axis])
Repeats each element repeats times (an integer, or one count per element along axis). Without axis the input is flattened first.
Parameters
repeatsnumber | number[]- Non-negative repetition counts.
[axis]number | null- Axis to repeat along.
Returns
NDArray
Example
np.repeat([[1, 2], [3, 4]], 2); // => [1, 1, 2, 2, 3, 3, 4, 4]
np.repeat([[1, 2], [3, 4]], [1, 2], 0); // => [[1, 2], [3, 4], [3, 4]]
np.array([1, 2]).repeat(2); // => [1, 1, 2, 2]TypeScript declaration
np.repeat(a: ArrayLike, repeats: IntList, axis?: number | null | undefined): NDArray
a.repeat(repeats: number | NDArray | readonly number[], axis?: number | null | undefined): NDArraynp.pad
#np.pad(a, padWidth, [mode], [{constantValues, endValues, statLength, reflectType}])
Pads an array. Modes: constant (default), edge, linear_ramp, maximum, mean, median, minimum, reflect, symmetric, wrap, empty, or a JS function (vector, [before, after], axis, options) that fills each 1-d lane in place. padWidth is n, [before, after], one pair per axis, or {axis: width}. Statistics on integer arrays are rounded to the nearest integer.
Parameters
padWidthnumber | number[] | number[][] | object- Number of values padded before/after each axis.
[mode]string | function- Padding mode.
Returns
NDArray
Example
np.pad([1, 2, 3], [1, 2]); // => [0, 1, 2, 3, 0, 0]
np.pad([1, 2, 3], 1, "constant", { constantValues: [7, 8] }); // => [7, 1, 2, 3, 8]
np.pad([1, 2, 3], 2, "edge"); // => [1, 1, 1, 2, 3, 3, 3]
np.pad([1, 2, 3], 2, "reflect"); // => [3, 2, 1, 2, 3, 2, 1]
np.pad([1, 2, 3], 2, "symmetric"); // => [2, 1, 1, 2, 3, 3, 2]
np.pad([1, 2, 3], 2, "wrap"); // => [2, 3, 1, 2, 3, 1, 2]
np.pad([1, 2, 3], 1, "mean"); // => [2, 1, 2, 3, 2]
np.pad([0, 4], [2, 0], "linear_ramp", { endValues: 4 }); // => [4, 2, 0, 4]TypeScript declaration
np.pad(a: ArrayLike, padWidth: PadWidth, mode?: PadMode | PadFunction | undefined, options?: (PadOptions & Record<string, unknown>) | undefined): NDArraynp.append
#np.append(arr, values, [axis])
Appends values to arr (both flattened when axis is omitted); a new array.
Returns
NDArray
Example
np.append([1, 2], [[3, 4]]); // => [1, 2, 3, 4]
np.append([[1, 2]], [[3, 4]], 0); // => [[1, 2], [3, 4]]TypeScript declaration
np.append(arr: ArrayLike, values: ArrayLike, axis?: number | null | undefined): NDArraynp.insert
#np.insert(arr, obj, values, [axis])
Inserts values before index/indices obj (an integer, list, boolean mask or slice object {start, stop, step}). Without axis the array is flattened. Values are cast to arr.dtype.
Returns
NDArray
Example
np.insert([1, 2, 3], 1, 9); // => [1, 9, 2, 3]
np.insert([[1, 1], [2, 2]], 1, 5, 1); // => [[1, 5, 1], [2, 5, 2]]
np.insert(np.arange(4), { start: 1, stop: 3 }, 0); // => [0, 0, 1, 0, 2, 3]TypeScript declaration
np.insert(arr: ArrayLike, obj: EditIndex, values: ArrayLike, axis?: number | null | undefined): NDArraynp.delete
#np.delete(arr, obj, [axis])
Removes the entries at obj (integer, list, boolean mask or slice object) along axis, flattening first when axis is omitted. Named export delete (implemented as del).
Returns
NDArray
Example
np.delete([1, 2, 3, 4], [0, -1]); // => [2, 3]
np.delete([[1, 2], [3, 4]], 0, 1); // => [[2], [4]]
np.delete(np.arange(6), { start: 0, step: 2 }); // => [1, 3, 5]TypeScript declaration
np.delete(arr: ArrayLike, obj: EditIndex, axis?: number | null | undefined): NDArraynp.resize
#np.resize(a, newShape) / a.resize(newShape, [{refcheck}])
np.resize returns a new array filled by repeating a's data. The method a.resize changes a itself (in place, returns undefined): data is truncated or zero-filled in memory order; a must own contiguous data, and while other arrays (views) still share it a ValueError is raised unless refcheck: false.
Returns
NDArray / undefined
Example
np.resize([1, 2, 3], [2, 4]); // => [[1, 2, 3, 1], [2, 3, 1, 2]]
const x = np.array([1, 2, 3]);
x.resize([5]);
x.toArray(); // => [1, 2, 3, 0, 0]TypeScript declaration
np.resize(a: ArrayLike, newShape: number | readonly number[]): NDArray
a.resize(newShape: number | readonly number[], options?: ResizeOptions | undefined): void
a.resize(...dims: number[]): voidnp.trimZeros
#np.trimZeros(filt, [trim], [axis])
Trims leading ("f") and/or trailing ("b") zeros (default "fb"). N-d input is trimmed to the bounding box of the nonzero values on the selected axes. Returns a view.
Returns
NDArray
Example
np.trimZeros([0, 0, 1, 0, 2, 0]); // => [1, 0, 2]
np.trimZeros([0, 0, 1, 0, 2, 0], "b"); // => [0, 0, 1, 0, 2]
np.trimZeros([[0, 0], [0, 3]]); // => [[3]]TypeScript declaration
np.trimZeros(filt: ArrayLike, trim?: string | undefined, axis?: number | readonly number[] | null | undefined): NDArraynp.flip
#np.flip(m, [axis])
Reverses element order along axis (an integer or list; all axes when omitted). fliplr reverses axis 1 and flipud axis 0. Views with negative strides.
Returns
NDArray
Example
np.flip([[1, 2], [3, 4]]); // => [[4, 3], [2, 1]]
np.flip([[1, 2], [3, 4]], 1); // => [[2, 1], [4, 3]]
np.fliplr([[1, 2], [3, 4]]); // => [[2, 1], [4, 3]]
np.flipud([[1, 2], [3, 4]]); // => [[3, 4], [1, 2]]TypeScript declaration
np.flip(m: ArrayLike, axis?: Axes | null | undefined): NDArraynp.roll
#np.roll(a, shift, [axis])
Shifts elements cyclically. Without axis the flattened array is rolled and the shape restored; shift and axis may be lists (broadcast against each other, shifts on the same axis add up). Returns a copy.
Returns
NDArray
Example
np.roll([1, 2, 3, 4], 1); // => [4, 1, 2, 3]
np.roll([[1, 2], [3, 4]], 1); // => [[4, 1], [2, 3]]
np.roll([[1, 2], [3, 4]], [1, 1], [0, 1]); // => [[4, 3], [2, 1]]TypeScript declaration
np.roll(a: ArrayLike, shift: Axes, axis?: Axes | null | undefined): NDArraynp.rot90
#np.rot90(m, [k], [axes])
Rotates by 90° k times (default 1) in the plane of axes (default [0, 1]), from the first axis towards the second. A view.
Returns
NDArray
Example
np.rot90([[1, 2], [3, 4]]); // => [[2, 4], [1, 3]]
np.rot90([[1, 2], [3, 4]], 2); // => [[4, 3], [2, 1]]TypeScript declaration
np.rot90(m: ArrayLike, k?: number | undefined, axes?: readonly number[] | undefined): NDArraynp.rollaxis
#np.rollaxis(a, axis, [start])
Moves axis so that it lies before position start (default 0). Prefer moveAxis. permuteDims(a, axes) permutes axes (array API transpose) and matrixTranspose(x) swaps the last two axes. All return views.
Returns
NDArray
Example
np.rollaxis(np.zeros([3, 4, 5]), 2).shape; // => [5, 3, 4]
np.permuteDims(np.zeros([2, 3, 4]), [2, 0, 1]).shape; // => [4, 2, 3]
np.matrixTranspose(np.zeros([2, 3, 4])).shape; // => [2, 4, 3]TypeScript declaration
np.rollaxis(a: ArrayLike, axis: number, start?: number | undefined): NDArraynp.copyto
#np.copyto(dst, src, [{casting, where}])
Copies src (broadcast to dst's shape) into dst in place. casting defaults to "same_kind"; JS scalars follow NEP 50 (an integer must fit an integer dst). where is a boolean mask; only elements where it is true are written.
Returns
undefined
Example
const d = np.zeros([2, 3]);
np.copyto(d, [1, 2, 3], { where: [true, false, true] });
d.toArray(); // => [[1, 0, 3], [1, 0, 3]]TypeScript declaration
np.copyto(dst: NDArray, src: number | bigint | boolean | NDArray | Complex | { readonly re: number; readonly im?: number | undefined; } | readonly NestedArray[], options?: CopytoOptions | undefined): voidnp.broadcastArrays
#np.broadcastArrays(...arrays)
Broadcasts the inputs against each other and returns views of the common shape (inputs already of that shape are returned as is).
Returns
NDArray[]
Example
np.broadcastArrays([1, 2, 3], [[1], [2]]).map((x) => x.shape); // => [[2, 3], [2, 3]]TypeScript declaration
np.broadcastArrays(...args: ArrayLike[]): NDArray[]np.asanyarray
#np.asanyarray(a, [{dtype}]) / np.asarrayChkfinite(a, [{dtype}])
asanyarray is asarray (numera has no array subclasses). asarrayChkfinite also raises ValueError if the result contains NaN or an infinity.
Returns
NDArray
Example
np.asanyarray([1, 2]).dtype.name; // => "int64"
np.asarrayChkfinite([1, 2]).size; // => 2TypeScript declaration
np.asanyarray(a: ArrayLike, options?: { dtype?: DTypeLike | undefined; } | undefined): NDArray
np.asarrayChkfinite(a: ArrayLike, options?: { dtype?: DTypeLike | undefined; } | undefined): NDArraynp.require
#np.require(a, [dtype], [requirements])
Returns a as an array of dtype that satisfies the requirement flags, copying only if needed: C/C_CONTIGUOUS/CONTIGUOUS, F/F_CONTIGUOUS/FORTRAN, A/ALIGNED, W/WRITEABLE, O/OWNDATA, E/ENSUREARRAY, as a string of letters or a list.
Returns
NDArray
Example
np.require([[1, 2], [3, 4]], "float32", ["F", "W"]).flags.fContiguous; // => trueTypeScript declaration
np.require(a: ArrayLike, dtype?: DTypeLike | null | undefined, requirements?: Requirements | null | undefined): NDArraynp.shape
#np.shape(a) / np.size(a, [axis]) / np.ndim(a) / np.isfortran(a)
Metadata of any array-like: shape, number of elements (or the length of axis), number of dimensions. isfortran is true for arrays that are F- but not C-contiguous.
Returns
number[] / number / boolean
Example
np.shape([[1, 2, 3]]); // => [1, 3]
np.size([[1, 2, 3]], 1); // => 3
np.ndim(5); // => 0
np.isfortran(np.zeros([2, 3], { order: "F" })); // => trueTypeScript declaration
np.shape(a: ArrayLike): number[]
np.size(a: ArrayLike, axis?: number | readonly number[] | null | undefined): number
np.ndim(a: ArrayLike): number
np.isfortran(a: NDArray): booleannp.applyAlongAxis
#np.applyAlongAxis(func1d, axis, arr, ...args)
Calls the JS function func1d(lane, ...args) on every 1-d lane of arr along axis. Results must all have the first result's shape; they replace that axis (scalars remove it).
Returns
NDArray
Example
np.applyAlongAxis((v) => v.sum(), 1, [[1, 2], [3, 4]]); // => [3, 7]
np.applyAlongAxis((v) => np.flip(v), 1, [[1, 2], [3, 4]]); // => [[2, 1], [4, 3]]TypeScript declaration
np.applyAlongAxis<A extends unknown[]>(func1d: (lane: NDArray, ...args: A) => ArrayLike, axis: number, arr: ArrayLike, ...args: A): NDArraynp.applyOverAxes
#np.applyOverAxes(func, a, axes)
Applies func(val, axis) for each axis in turn; a result with one dimension fewer gets the axis back as length 1 (like keepdims).
Returns
NDArray
Example
np.applyOverAxes((x, ax) => x.sum({ axis: ax }), np.arange(6).reshape(2, 3), [0, 1]); // => [[15]]TypeScript declaration
np.applyOverAxes(func: (a: NDArray, axis: number) => ArrayLike, a: ArrayLike, axes: number | readonly number[]): NDArray