Cyforanumera
npm

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()).

NameSummary
np.reshapeFunction form of a.reshape(shape).
np.transposePermutes the axes.
np.squeezeRemoves length-1 axes: all of them, or only the ones given.
np.expandDimsInserts length-1 axes at the given positions.
np.swapAxesSwaps two axes.
np.moveAxisMoves axes to new positions; the other axes keep their relative order.
np.ravelFlattens to 1-D.
np.broadcastToRead-only view of a broadcast to shape, without copying.
np.broadcastShapesThe shape that the given shapes broadcast to.
np.seterrSets how floating-point errors are handled: "ignore", "warn" (a Node RuntimeWarning), "raise" (FloatingPointError) or "print".
np.geterrThe current floating-point error settings.
np.errstateRuns fn synchronously with the given error settings and restores the previous ones afterwards.
np.concatenateJoins arrays along an existing axis (default 0).
np.stackJoins same-shaped arrays along a new axis.
np.vstackStacks inputs (made at least 2-d) along axis 0.
np.hstackJoins along axis 1, or axis 0 for 1-d inputs.
np.dstackJoins along axis 2 after making each input at least 3-d.
np.columnStackStacks 1-d arrays as columns of a 2-d array (2-d inputs are joined as they are).
np.blockAssembles 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.unstackSplits an array into a list of views along axis (default 0), removing that axis.
np.splitSplits into equal sections (must divide the axis) or at the given indices.
np.atleast1dViews with at least 1 (atleast1d), 2 (atleast2d, (N) → (1, N)) or 3 (atleast3d, (N) → (1, N, 1), (M, N) → (M, N, 1)) dimensions.
np.tileRepeats 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.repeatRepeats each element repeats times (an integer, or one count per element along axis).
np.padPads an array.
np.appendAppends values to arr (both flattened when axis is omitted); a new array.
np.insertInserts values before index/indices obj (an integer, list, boolean mask or slice object {start, stop, step}).
np.deleteRemoves the entries at obj (integer, list, boolean mask or slice object) along axis, flattening first when axis is omitted.
np.resizenp.resize returns a new array filled by repeating a's data.
np.trimZerosTrims leading ("f") and/or trailing ("b") zeros (default "fb").
np.flipReverses element order along axis (an integer or list; all axes when omitted).
np.rollShifts elements cyclically.
np.rot90Rotates by 90° k times (default 1) in the plane of axes (default [0, 1]), from the first axis towards the second.
np.rollaxisMoves axis so that it lies before position start (default 0).
np.copytoCopies src (broadcast to dst's shape) into dst in place.
np.broadcastArraysBroadcasts the inputs against each other and returns views of the common shape (inputs already of that shape are returned as is).
np.asanyarrayasanyarray is asarray (numera has no array subclasses).
np.requireReturns 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.shapeMetadata of any array-like: shape, number of elements (or the length of axis), number of dimensions.
np.applyAlongAxisCalls the JS function func1d(lane, ...args) on every 1-d lane of arr along axis.
np.applyOverAxesApplies 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

TypeScript
np.reshape(np.arange(4), [2, 2]); // => [[0, 1], [2, 3]]
TypeScript declaration
np.reshape(a: NDArray, shape: number | Shape, opts?: OrderOptions | undefined): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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[]): NDArray

np.swapAxes

#
np.swapAxes(a, axis1, axis2)

Swaps two axes.

Parameters

aNDArray
Input array.
axis1, axis2number
The axes to swap.

Returns

NDArray

Example

TypeScript
np.swapAxes(np.zeros([2, 3, 4]), 0, 2).shape; // => [4, 3, 2]
TypeScript declaration
np.swapAxes(a: NDArray, axis1: number, axis2: number): NDArray

np.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

TypeScript
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[]): NDArray

np.ravel

#
np.ravel(a)

Flattens to 1-D. Returns a view when possible, otherwise a copy.

Parameters

aNDArray
Input array.

Returns

NDArray

Example

TypeScript
np.ravel(np.array([[1, 2], [3, 4]])); // => [1, 2, 3, 4]
TypeScript declaration
np.ravel(a: NDArray, opts?: OrderOptions | undefined): NDArray

np.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

TypeScript
np.broadcastTo([1, 2], [2, 2]); // => [[1, 2], [1, 2]]
TypeScript declaration
np.broadcastTo(a: ArrayLike, shape: number | Shape): NDArray

np.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

TypeScript
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; all sets every category.

Returns

object

Example

TypeScript
const old = np.seterr({ all: "ignore" });
np.seterr(old).divide; // => "ignore"
TypeScript declaration
np.seterr(settings?: ErrSettings | undefined): ErrState

np.geterr

#
np.geterr()

The current floating-point error settings.

Returns

object

Example

TypeScript
np.geterr().under; // => "ignore"
TypeScript declaration
np.geterr(): ErrState

np.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

TypeScript
np.errstate({ divide: "ignore" }, () => np.divide([1], [0]).toArray()[0] === Infinity); // => true
TypeScript declaration
np.errstate<T>(settings: ErrSettings, fn: () => T): T

np.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

TypeScript
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): NDArray

np.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

TypeScript
np.stack([[1, 2], [3, 4]], 1); // => [[1, 3], [2, 4]]
TypeScript declaration
np.stack(arrays: Sequence, axis?: number | StackOptions | undefined, options?: JoinOptions | undefined): NDArray

np.vstack

#
np.vstack(arrays, [{dtype, casting}])

Stacks inputs (made at least 2-d) along axis 0.

Returns

NDArray

Example

TypeScript
np.vstack([[1, 2], [3, 4]]); // => [[1, 2], [3, 4]]
TypeScript declaration
np.vstack(arrays: Sequence, options?: VHStackOptions | undefined): NDArray

np.hstack

#
np.hstack(arrays, [{dtype, casting}])

Joins along axis 1, or axis 0 for 1-d inputs.

Returns

NDArray

Example

TypeScript
np.hstack([[1, 2], [3]]); // => [1, 2, 3]
TypeScript declaration
np.hstack(arrays: Sequence, options?: VHStackOptions | undefined): NDArray

np.dstack

#
np.dstack(arrays)

Joins along axis 2 after making each input at least 3-d.

Returns

NDArray

Example

TypeScript
np.dstack([[1, 2], [3, 4]]); // => [[[1, 3], [2, 4]]]
TypeScript declaration
np.dstack(arrays: Sequence): NDArray

np.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

TypeScript
np.columnStack([[1, 2], [3, 4]]); // => [[1, 3], [2, 4]]
TypeScript declaration
np.columnStack(arrays: Sequence): NDArray

np.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

TypeScript
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): NDArray

np.unstack

#
np.unstack(x, [axis])

Splits an array into a list of views along axis (default 0), removing that axis.

Returns

NDArray[]

Example

TypeScript
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

TypeScript
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

TypeScript
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; // => 2
TypeScript 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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.append

#
np.append(arr, values, [axis])

Appends values to arr (both flattened when axis is omitted); a new array.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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[]): void

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): void

np.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

TypeScript
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

TypeScript
np.asanyarray([1, 2]).dtype.name;   // => "int64"
np.asarrayChkfinite([1, 2]).size;    // => 2
TypeScript declaration
np.asanyarray(a: ArrayLike, options?: { dtype?: DTypeLike | undefined; } | undefined): NDArray
np.asarrayChkfinite(a: ArrayLike, options?: { dtype?: DTypeLike | undefined; } | undefined): NDArray

np.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

TypeScript
np.require([[1, 2], [3, 4]], "float32", ["F", "W"]).flags.fContiguous; // => true
TypeScript declaration
np.require(a: ArrayLike, dtype?: DTypeLike | null | undefined, requirements?: Requirements | null | undefined): NDArray

np.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

TypeScript
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" })); // => true
TypeScript 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): boolean

np.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

TypeScript
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): NDArray

np.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

TypeScript
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