API reference
Array creation
| Name | Summary |
|---|---|
np.array | Creates an array from nested JS arrays, numbers, booleans or bigints. |
np.asarray | Like array, but returns the input unchanged (no copy) if it is already an NDArray of the requested dtype. |
np.zeros | Array filled with zeros. |
np.ones | Array filled with ones. |
np.empty | Array whose contents are uninitialized. |
np.full | Array filled with fillValue. |
np.zerosLike | Zeros with the same shape and dtype as a. |
np.arange | Evenly spaced values in the half-open interval [start, stop). |
np.linspace | num evenly spaced samples from start to stop. |
np.eye | 2-D array with ones on a diagonal and zeros elsewhere. |
np.identity | Square identity matrix, the same as eye(n). |
np.fromTypedArray | Copies a TypedArray (or Node Buffer) into an array. |
np.logspace | num numbers spaced evenly on a log scale: base ** linspace(start, stop, num). |
np.geomspace | num numbers forming a geometric progression from start to stop (endpoints exact). |
np.tri | An n × m array with ones at and below the k-th diagonal and zeros elsewhere. |
np.tril | Copy of m with the elements above the k-th diagonal zeroed. |
np.triu | Copy of m with the elements below the k-th diagonal zeroed. |
np.diag | For a 2-D input, returns the k-th diagonal as a read-only view. |
np.diagflat | Flattens v and builds a 2-D array with it on the k-th diagonal. |
np.vander | Vandermonde matrix: column j is x ** (n - 1 - j) (or x ** j when increasing). |
np.fromfunction | Calls fn once with one coordinate array per axis (like np.indices) and returns its result. |
np.fromiter | Builds a 1-D array from any JS iterable. |
np.frombuffer | Interprets raw bytes (ArrayBuffer, typed array or DataView) as a 1-D array. |
np.fromstring | Parses separated numbers from text (NumPy's text mode). |
np.astype | Function 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
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): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
const a = np.array([1, 2, 3]);
np.asarray(a) === a; // => trueTypeScript declaration
np.asarray(data: NDArray | NestedArray, options?: ArrayOptions | undefined): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
np.zeros([2, 3]); // => [[0, 0, 0], [0, 0, 0]]TypeScript declaration
np.zeros(shape: number | Shape, options?: CreationOptions | undefined): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
np.ones(3, { dtype: "int32" }); // => [1, 1, 1]TypeScript declaration
np.ones(shape: number | Shape, options?: CreationOptions | undefined): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
np.empty([2, 2]).shape; // => [2, 2]TypeScript declaration
np.empty(shape: number | Shape, options?: CreationOptions | undefined): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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): NDArraynp.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
endpointisfalse). [num=50]number- Number of samples.
[options.endpoint]boolean- Include
stop. Defaulttrue. [options.dtype]DTypeLike- Element type, e.g.
"float32"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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): NDArraynp.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:
0main, positive above, negative below. [options.dtype]DTypeLike- Element type, e.g.
"float32"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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): NDArraynp.identity
#np.identity(n, [options])
Square identity matrix, the same as eye(n).
Parameters
nnumber- Size.
[options.dtype]DTypeLike- Element type, e.g.
"float32"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
np.identity(3).toArray()[1]; // => [0, 1, 0]TypeScript declaration
np.identity(n: number, options?: ArrayOptions | undefined): NDArraynp.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"ornp.int32. Defaultfloat64.
Returns
NDArray
Example
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. Defaulttrue. [options.base]number- Base of the log space. Default
10. [options.dtype]DTypeLike- Element type. Default
float64(complex inputs givecomplex128).
Returns
NDArray
Example
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): NDArraynp.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. Defaulttrue. [options.dtype]DTypeLike- Element type. Default
float64.
Returns
NDArray
Example
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): NDArraynp.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
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): NDArraynp.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
NDArrayor nested JS array. [k]number- Diagonal offset:
0main,> 0above,< 0below. Default0.
Returns
NDArray
Example
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): NDArraynp.triu
#np.triu(m, [k])
Copy of m with the elements below the k-th diagonal zeroed.
Parameters
mArrayLike- An
NDArrayor nested JS array. [k]number- Diagonal offset:
0main,> 0above,< 0below. Default0.
Returns
NDArray
Example
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): NDArraynp.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
NDArrayor nested JS array. [k]number- Diagonal offset:
0main,> 0above,< 0below. Default0.
Returns
NDArray
Example
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): NDArraynp.diagflat
#np.diagflat(v, [k])
Flattens v and builds a 2-D array with it on the k-th diagonal.
Parameters
vArrayLike- An
NDArrayor nested JS array. [k]number- Diagonal offset:
0main,> 0above,< 0below. Default0.
Returns
NDArray
Example
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): NDArraynp.vander
#np.vander(x, [n], [options])
Vandermonde matrix: column j is x ** (n - 1 - j) (or x ** j when increasing).
Parameters
xArrayLike- An
NDArrayor 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
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): NDArraynp.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
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): Rnp.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
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): NDArraynp.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
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): NDArraynp.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
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): NDArraynp.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;falsereturnsxwhen no cast is needed.
Returns
NDArray
Example
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