Cyforanumera
npm

API reference

Array creation

NameSummary
np.arrayCreates an array from nested JS arrays, numbers, booleans or bigints.
np.asarrayLike array, but returns the input unchanged (no copy) if it is already an NDArray of the requested dtype.
np.zerosArray filled with zeros.
np.onesArray filled with ones.
np.emptyArray whose contents are uninitialized.
np.fullArray filled with fillValue.
np.zerosLikeZeros with the same shape and dtype as a.
np.arangeEvenly spaced values in the half-open interval [start, stop).
np.linspacenum evenly spaced samples from start to stop.
np.eye2-D array with ones on a diagonal and zeros elsewhere.
np.identitySquare identity matrix, the same as eye(n).
np.fromTypedArrayCopies a TypedArray (or Node Buffer) into an array.
np.logspacenum numbers spaced evenly on a log scale: base ** linspace(start, stop, num).
np.geomspacenum numbers forming a geometric progression from start to stop (endpoints exact).
np.triAn n × m array with ones at and below the k-th diagonal and zeros elsewhere.
np.trilCopy of m with the elements above the k-th diagonal zeroed.
np.triuCopy of m with the elements below the k-th diagonal zeroed.
np.diagFor a 2-D input, returns the k-th diagonal as a read-only view.
np.diagflatFlattens v and builds a 2-D array with it on the k-th diagonal.
np.vanderVandermonde matrix: column j is x ** (n - 1 - j) (or x ** j when increasing).
np.fromfunctionCalls fn once with one coordinate array per axis (like np.indices) and returns its result.
np.fromiterBuilds a 1-D array from any JS iterable.
np.frombufferInterprets raw bytes (ArrayBuffer, typed array or DataView) as a 1-D array.
np.fromstringParses separated numbers from text (NumPy's text mode).
np.astypeFunction form of a.astype (array API).

np.array

#
np.array(data, [options])

Creates an array from nested JS arrays, numbers, booleans or bigints. The dtype is inferred like NumPy: all booleans → bool, all integers → int64, otherwise float64.

Parameters

dataNestedArray | NDArray
Values to copy into the array.
[options.dtype]DTypeLike
Force a dtype instead of inferring it.

Returns

NDArray

Example

TypeScript
const a = np.array([[1, 2], [3, 4]]);
a.shape;       // => [2, 2]
a.dtype.name;  // => "int64"
np.array([1, 2], { dtype: "float32" }).dtype.name; // => "float32"
TypeScript declaration
np.array(data: NDArray | NestedArray, options?: ArrayCopyOptions | undefined): NDArray

np.asarray

#
np.asarray(data, [options])

Like array, but returns the input unchanged (no copy) if it is already an NDArray of the requested dtype.

Parameters

dataNestedArray | NDArray
Input data.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
const a = np.array([1, 2, 3]);
np.asarray(a) === a; // => true
TypeScript declaration
np.asarray(data: NDArray | NestedArray, options?: ArrayOptions | undefined): NDArray

np.zeros

#
np.zeros(shape, [options])

Array filled with zeros.

Parameters

shapenumber | number[]
Dimensions of the new array.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.zeros([2, 3]); // => [[0, 0, 0], [0, 0, 0]]
TypeScript declaration
np.zeros(shape: number | Shape, options?: CreationOptions | undefined): NDArray

np.ones

#
np.ones(shape, [options])

Array filled with ones.

Parameters

shapenumber | number[]
Dimensions of the new array.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.ones(3, { dtype: "int32" }); // => [1, 1, 1]
TypeScript declaration
np.ones(shape: number | Shape, options?: CreationOptions | undefined): NDArray

np.empty

#
np.empty(shape, [options])

Array whose contents are uninitialized. It is faster than zeros when you are going to overwrite every element anyway.

Parameters

shapenumber | number[]
Dimensions of the new array.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.empty([2, 2]).shape; // => [2, 2]
TypeScript declaration
np.empty(shape: number | Shape, options?: CreationOptions | undefined): NDArray

np.full

#
np.full(shape, fillValue, [options])

Array filled with fillValue. Without dtype, the dtype is inferred from the value.

Parameters

shapenumber | number[]
Dimensions of the new array.
fillValuenumber | boolean | bigint
Value for every element.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.full([2, 2], 7); // => [[7, 7], [7, 7]]
np.full(2, 1.5).dtype.name; // => "float64"
TypeScript declaration
np.full(shape: number | Shape, fillValue: number | bigint | boolean | ComplexLike, options?: CreationOptions | undefined): NDArray

np.zerosLike

#
np.zerosLike(a, [options])

Zeros with the same shape and dtype as a. The same family has onesLike(a), emptyLike(a) and fullLike(a, fillValue).

Parameters

aNDArray
Template array.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
const a = np.array([[1, 2], [3, 4]]);
np.zerosLike(a);      // => [[0, 0], [0, 0]]
np.fullLike(a, 9);    // => [[9, 9], [9, 9]]
np.onesLike(a).dtype.name; // => "int64"
TypeScript declaration
np.zerosLike(a: NDArray, options?: LikeOptions | undefined): NDArray

np.arange

#
np.arange([start], stop, [step], [options])

Evenly spaced values in the half-open interval [start, stop). With one argument, it is stop. The result is int64 when every argument is an integer, and float64 otherwise. options is always the 4th argument: np.arange(0, 5, 1, { dtype: "float32" }).

Parameters

[start]number
Start of the interval. Default 0.
stopnumber
End of the interval (excluded).
[step]number
Spacing between values. Default 1.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.arange(5);        // => [0, 1, 2, 3, 4]
np.arange(2, 10, 3); // => [2, 5, 8]
np.arange(1, 0, -0.25); // => [1, 0.75, 0.5, 0.25]
TypeScript declaration
np.arange(startOrStop: number, stop?: number | undefined, step?: number | undefined, options?: ArrayOptions | undefined): NDArray

np.linspace

#
np.linspace(start, stop, [num=50], [options])

num evenly spaced samples from start to stop.

Parameters

startnumber
First value.
stopnumber
Last value (included unless endpoint is false).
[num=50]number
Number of samples.
[options.endpoint]boolean
Include stop. Default true.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.linspace(0, 1, 5); // => [0, 0.25, 0.5, 0.75, 1]
np.linspace(0, 1, 4, { endpoint: false }); // => [0, 0.25, 0.5, 0.75]
TypeScript declaration
np.linspace(start: number, stop: number, num?: number | undefined, options?: LinspaceOptions | undefined): NDArray

np.eye

#
np.eye(n, [m=n], [options])

2-D array with ones on a diagonal and zeros elsewhere.

Parameters

nnumber
Number of rows.
[m]number
Number of columns. Default n.
[options.k]number
Diagonal offset: 0 main, positive above, negative below.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.eye(2);                        // => [[1, 0], [0, 1]]
np.eye(2, 3, { k: 1, dtype: "int32" }); // => [[0, 1, 0], [0, 0, 1]]
TypeScript declaration
np.eye(n: number, m?: number | undefined, options?: EyeOptions | undefined): NDArray

np.identity

#
np.identity(n, [options])

Square identity matrix, the same as eye(n).

Parameters

nnumber
Size.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
np.identity(3).toArray()[1]; // => [0, 1, 0]
TypeScript declaration
np.identity(n: number, options?: ArrayOptions | undefined): NDArray

np.fromTypedArray

#
np.fromTypedArray(data, [shape], [options])

Copies a TypedArray (or Node Buffer) into an array. The dtype defaults to the element type, e.g. Float32Array → float32. An explicit dtype reinterprets the raw bytes.

Parameters

dataArrayBufferView
Source data (copied).
[shape]number | number[]
Target shape. Default: 1-D.
[options.dtype]DTypeLike
Element type, e.g. "float32" or np.int32. Default float64.

Returns

NDArray

Example

TypeScript
const a = np.fromTypedArray(new Int32Array([1, 2, 3, 4]), [2, 2]);
a.dtype.name; // => "int32"
a;            // => [[1, 2], [3, 4]]

np.logspace

#
np.logspace(start, stop, [num], [options])

num numbers spaced evenly on a log scale: base ** linspace(start, stop, num). start/stop are scalars (real or complex); array bounds and axis are not supported.

Parameters

startnumber | Complex
Exponent of the first value.
stopnumber | Complex
Exponent of the last value.
[num]number
Number of samples. Default 50.
[options.endpoint]boolean
Include base ** stop. Default true.
[options.base]number
Base of the log space. Default 10.
[options.dtype]DTypeLike
Element type. Default float64 (complex inputs give complex128).

Returns

NDArray

Example

TypeScript
np.logspace(0, 2, 3);                    // => [1, 10, 100]
np.logspace(0, 3, 4, { base: 2 });        // => [1, 2, 4, 8]
np.logspace(0, 2.5, 3, { dtype: "int64" }); // => [1, 17, 316]
TypeScript declaration
np.logspace(start: ScalarLike, stop: ScalarLike, num?: number | undefined, options?: LogspaceOptions | undefined): NDArray

np.geomspace

#
np.geomspace(start, stop, [num], [options])

num numbers forming a geometric progression from start to stop (endpoints exact). Zero bounds raise ValueError; a sign change gives NaN interior values like NumPy.

Parameters

startnumber | Complex
First value.
stopnumber | Complex
Last value.
[num]number
Number of samples. Default 50.
[options.endpoint]boolean
Include stop. Default true.
[options.dtype]DTypeLike
Element type. Default float64.

Returns

NDArray

Example

TypeScript
np.geomspace(1, 1000, 4);  // => [1, 10, 100, 1000]
np.geomspace(-1000, -1, 4); // => [-1000, -100, -10, -1]
TypeScript declaration
np.geomspace(start: ScalarLike, stop: ScalarLike, num?: number | undefined, options?: GeomspaceOptions | undefined): NDArray

np.tri

#
np.tri(n, [m], [options])

An n × m array with ones at and below the k-th diagonal and zeros elsewhere.

Parameters

nnumber
Rows.
[m]number | null
Columns. Default n.
[options.k]number
Diagonal offset. Default 0.
[options.dtype]DTypeLike
Element type. Default float64.

Returns

NDArray

Example

TypeScript
np.tri(3, 4, { k: 1 }); // => [[1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]]
TypeScript declaration
np.tri(n: number, m?: number | null | undefined, options?: TriOptions | undefined): NDArray

np.tril

#
np.tril(m, [k])

Copy of m with the elements above the k-th diagonal zeroed. Applies to the last two axes of stacked input.

Parameters

mArrayLike
An NDArray or nested JS array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

NDArray

Example

TypeScript
np.tril([[1, 2, 3], [4, 5, 6], [7, 8, 9]], -1); // => [[0, 0, 0], [4, 0, 0], [7, 8, 0]]
TypeScript declaration
np.tril(m: ArrayInput, k?: number | undefined): NDArray

np.triu

#
np.triu(m, [k])

Copy of m with the elements below the k-th diagonal zeroed.

Parameters

mArrayLike
An NDArray or nested JS array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

NDArray

Example

TypeScript
np.triu([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 1); // => [[0, 2, 3], [0, 0, 6], [0, 0, 0]]
TypeScript declaration
np.triu(m: ArrayInput, k?: number | undefined): NDArray

np.diag

#
np.diag(v, [k])

For a 2-D input, returns the k-th diagonal as a read-only view. For a 1-D input, builds a 2-D array with v on the k-th diagonal.

Parameters

vArrayLike
An NDArray or nested JS array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

NDArray

Example

TypeScript
np.diag([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // => [1, 5, 9]
np.diag([1, 2], 1); // => [[0, 1, 0], [0, 0, 2], [0, 0, 0]]
TypeScript declaration
np.diag(v: ArrayInput, k?: number | undefined): NDArray

np.diagflat

#
np.diagflat(v, [k])

Flattens v and builds a 2-D array with it on the k-th diagonal.

Parameters

vArrayLike
An NDArray or nested JS array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

NDArray

Example

TypeScript
np.diagflat([[1, 2], [3, 4]]).shape; // => [4, 4]
np.diagflat([1, 2], -1); // => [[0, 0, 0], [1, 0, 0], [0, 2, 0]]
TypeScript declaration
np.diagflat(v: ArrayInput, k?: number | undefined): NDArray

np.vander

#
np.vander(x, [n], [options])

Vandermonde matrix: column j is x ** (n - 1 - j) (or x ** j when increasing).

Parameters

xArrayLike
An NDArray or nested JS array.
[n]number | null
Number of columns. Default x.length.
[options.increasing]boolean
Increasing powers left to right. Default false.

Returns

NDArray

Example

TypeScript
np.vander([1, 2, 3], 3); // => [[1, 1, 1], [4, 2, 1], [9, 3, 1]]
np.vander([1, 2, 3], null, { increasing: true }); // => [[1, 1, 1], [1, 2, 4], [1, 3, 9]]
TypeScript declaration
np.vander(x: ArrayInput, n?: number | null | undefined, options?: { increasing?: boolean | undefined; } | undefined): NDArray

np.fromfunction

#
np.fromfunction(fn, shape, [options])

Calls fn once with one coordinate array per axis (like np.indices) and returns its result.

Parameters

fn(...coords: NDArray[]) => R
Function of the coordinate arrays.
shapenumber[]
Grid shape.
[options.dtype]DTypeLike
Element type. Default float64 (dtype of the coordinate arrays).

Returns

R

Example

TypeScript
np.fromfunction((i, j) => np.add(i, j), [2, 3]); // => [[0, 1, 2], [1, 2, 3]]
TypeScript declaration
np.fromfunction<R>(fn: (...coords: NDArray[]) => R, shape: Shape, options?: { dtype?: DTypeLike | undefined; } | undefined): R

np.fromiter

#
np.fromiter(iterable, dtype, [count])

Builds a 1-D array from any JS iterable. count reads at most that many items and raises ValueError if the iterator is shorter.

Parameters

iterableIterable<number | boolean | bigint | Complex>
Source values.
dtypeDTypeLike
Element type (required).
[count]number
Items to read; -1 (default) reads all.

Returns

NDArray

Example

TypeScript
np.fromiter(new Set([1, 2, 3]), "float32"); // => [1, 2, 3]
np.fromiter([5, 6, 7, 8], "int64", 2); // => [5, 6]
TypeScript declaration
np.fromiter(iterable: Iterable<number | bigint | boolean | ComplexLike>, dtype: DTypeLike, count?: number | undefined): NDArray

np.frombuffer

#
np.frombuffer(buffer, [options])

Interprets raw bytes (ArrayBuffer, typed array or DataView) as a 1-D array. The data is copied; byte order is native.

Parameters

bufferArrayBufferLike | ArrayBufferView
Source bytes.
[options.dtype]DTypeLike
Element type. Default float64.
[options.count]number
Items to read; -1 (default) reads all.
[options.offset]number
Start offset in bytes. Default 0.

Returns

NDArray

Example

TypeScript
np.frombuffer(new Int16Array([1, 2, 3]), { dtype: "int16", offset: 2 }); // => [2, 3]
np.frombuffer(new Uint8Array([1, 2, 3]).buffer, { dtype: "uint8", count: 2 }); // => [1, 2]
TypeScript declaration
np.frombuffer(buffer: ArrayBufferLike | ArrayBufferView<ArrayBufferLike>, options?: FrombufferOptions | undefined): NDArray

np.fromstring

#
np.fromstring(text, [options])

Parses separated numbers from text (NumPy's text mode). An empty sep (binary mode) raises ValueError; use frombuffer. Whitespace in sep matches any run of whitespace.

Parameters

textstring
Text to parse.
[options.dtype]DTypeLike
Element type. Default float64.
[options.count]number
Items to read; -1 (default) reads all.
[options.sep]string
Separator (required, non-empty).

Returns

NDArray

Example

TypeScript
np.fromstring("1 2 3", { sep: " ", dtype: "int64" }); // => [1, 2, 3]
np.fromstring("1.5, 2, 3", { sep: "," }); // => [1.5, 2, 3]
TypeScript declaration
np.fromstring(text: string, options?: FromstringOptions | undefined): NDArray

np.astype

#
np.astype(x, dtype, [options])

Function form of a.astype (array API). x must be an NDArray.

Parameters

xNDArray
Input array.
dtypeDTypeLike
Target dtype.
[options.copy]boolean
Default true; false returns x when no cast is needed.

Returns

NDArray

Example

TypeScript
np.astype(np.array([1.7, -2.2]), "int32"); // => [1, -2]
TypeScript declaration
np.astype(x: NDArray, dtype: DTypeLike, options?: Pick<AstypeOptions, "copy"> | undefined): NDArray