API reference
Grids and index helpers
JS has no obj[...] overloading, so NumPy's index-trick objects (mgrid, ogrid, r_, c_, s_, index_exp) are functions here. Slices are [start, stop, step] tuples or strings like "1:4".
| Name | Summary |
|---|---|
np.indices | An (N, ...dimensions) array whose i-th sub-array holds the index along axis i. |
np.meshgrid | Coordinate matrices from coordinate vectors. |
np.mgrid | Dense multi-dimensional grid; np.mgrid([0, 2], [0, 3]) is NumPy's np.mgrid[0:2, 0:3]. |
np.ogrid | Open (sparse) version of mgrid: one broadcastable array per slice. |
np.ix_ | Open mesh from 1-D sequences for cross-product indexing. |
np.r_ | Concatenates along the first axis. |
np.c_ | Like r_, but stacks 1-D inputs as columns (directive "-1,2,0"). |
np.s_ | Builds index specs for a.get(...): slice strings become [start, stop, step] tuples and "..." stays the ellipsis. |
np.indexExp | NumPy index_exp: like s_, but always returns an array of specs. |
np.indices
#np.indices(dimensions, [options])
An (N, ...dimensions) array whose i-th sub-array holds the index along axis i. With sparse: true, returns one broadcastable array per axis.
Parameters
dimensionsnumber[]- Grid shape.
[options.dtype]DTypeLike- Element type. Default
int64. [options.sparse]boolean- Return a list of sparse arrays. Default
false.
Returns
NDArray | NDArray[]
Example
np.indices([2, 3]); // => [[[0, 0, 0], [1, 1, 1]], [[0, 1, 2], [0, 1, 2]]]
np.indices([2, 3], { sparse: true }).map((a) => a.shape); // => [[2, 1], [1, 3]]TypeScript declaration
np.indices(dimensions: readonly number[], options: IndicesOptions & { sparse: true; }): NDArray[]
np.indices(dimensions: readonly number[], options?: IndicesOptions | undefined): NDArraynp.meshgrid
#np.meshgrid(...xi, [options])
Coordinate matrices from coordinate vectors. "xy" indexing swaps the first two output axes (Cartesian); "ij" keeps matrix order. copy: false returns read-only broadcast views.
Parameters
...xiArrayLike[]- 1-D coordinate vectors (flattened otherwise).
[options.indexing]"xy" | "ij"- Default
"xy". [options.sparse]boolean- Return broadcastable arrays instead of full grids. Default
false. [options.copy]boolean- Default
true.
Returns
NDArray[]
Example
const [X, Y] = np.meshgrid([1, 2, 3], [4, 5]);
X; // => [[1, 2, 3], [1, 2, 3]]
Y; // => [[4, 4, 4], [5, 5, 5]]
np.meshgrid([1, 2, 3], [4, 5], { indexing: "ij", sparse: true }).map((a) => a.shape); // => [[3, 1], [1, 2]]TypeScript declaration
np.meshgrid(...args: (ArrayInput | MeshgridOptions)[]): NDArray[]np.mgrid
#np.mgrid(...slices)
Dense multi-dimensional grid; np.mgrid([0, 2], [0, 3]) is NumPy's np.mgrid[0:2, 0:3]. A complex step np.complex(0, n) means n points with the stop included. One slice returns a 1-D array.
Parameters
...slices[start, stop, step?][]- One slice per axis;
startmay benull.
Returns
NDArray
Example
np.mgrid([0, 2], [0, 3]); // => [[[0, 0, 0], [1, 1, 1]], [[0, 1, 2], [0, 1, 2]]]
np.mgrid([-1, 1, np.complex(0, 5)]); // => [-1, -0.5, 0, 0.5, 1]TypeScript declaration
np.mgrid(...slices: GridSlice[]): NDArraynp.ogrid
#np.ogrid(...slices)
Open (sparse) version of mgrid: one broadcastable array per slice.
Parameters
...slices[start, stop, step?][]- One slice per axis.
Returns
NDArray[] (NDArray for a single slice)
Example
const [i, j] = np.ogrid([0, 2], [0, 3]);
i; // => [[0], [1]]
j; // => [[0, 1, 2]]TypeScript declaration
np.ogrid(slice: GridSlice): NDArray
np.ogrid(...slices: GridSlice[]): NDArray[]np.ix_
#np.ix_(...seqs)
Open mesh from 1-D sequences for cross-product indexing. Boolean sequences become their nonzero indices.
Parameters
...seqsArrayLike[]- 1-D integer or boolean sequences.
Returns
NDArray[]
Example
const [r, c] = np.ix_([0, 1], [2, 4]);
r; // => [[0], [1]]
c; // => [[2, 4]]
np.ix_([true, false, true])[0]; // => [0, 2]TypeScript declaration
np.ix_(...seqs: ArrayInput[]): NDArray[]np.r_
#np.r_(...items)
Concatenates along the first axis. Items are arrays, scalars or slice strings ("1:4", "0:1:5j"). A leading directive string such as "0,2" (axis, ndmin) or "1,2,0" (axis, ndmin, trans1d) works as in NumPy; the matrix directives "r"/"c" are not supported.
Parameters
...items(ArrayLike | string)[]- Pieces to join.
Returns
NDArray
Example
np.r_("1:4", 0, 4, [7, 8]); // => [1, 2, 3, 0, 4, 7, 8]
np.r_("0:1:5j"); // => [0, 0.25, 0.5, 0.75, 1]
np.r_("0,2", [1, 2], [3, 4]); // => [[1, 2], [3, 4]]TypeScript declaration
np.r_(...items: ConcatItem[]): NDArraynp.c_
#np.c_(...items)
Like r_, but stacks 1-D inputs as columns (directive "-1,2,0").
Parameters
...items(ArrayLike | string)[]- Pieces to join.
Returns
NDArray
Example
np.c_([1, 2, 3], [4, 5, 6]); // => [[1, 4], [2, 5], [3, 6]]TypeScript declaration
np.c_(...items: ConcatItem[]): NDArraynp.s_
#np.s_(...specs)
Builds index specs for a.get(...): slice strings become [start, stop, step] tuples and "..." stays the ellipsis. One spec returns the spec itself.
Parameters
...specs(IndexSpec | string)[]- Indices or slice strings.
Returns
IndexSpec | IndexSpec[]
Example
np.s_("1:3"); // => [1, 3, null]
np.s_(0, "::2"); // => [0, [null, null, 2]]
np.arange(6).get(np.s_("1:5:2")); // => [1, 3]TypeScript declaration
np.s_(spec: string | number | boolean | NDArray | readonly [] | readonly [number | null] | readonly [number | null, number | null] | readonly [number | null, number | null, number | null] | null): IndexSpec
np.s_(...specs: (string | number | boolean | NDArray | readonly [] | readonly [number | null] | readonly [number | null, number | null] | readonly [number | null, number | null, number | null] | null)[]): IndexSpec[]np.indexExp
#np.indexExp(...specs)
NumPy index_exp: like s_, but always returns an array of specs.
Parameters
...specs(IndexSpec | string)[]- Indices or slice strings.
Returns
IndexSpec[]
Example
np.indexExp(1); // => [1]
np.indexExp("2:"); // => [[2, null, null]]TypeScript declaration
np.indexExp(...specs: (string | number | boolean | NDArray | readonly [] | readonly [number | null] | readonly [number | null, number | null] | readonly [number | null, number | null, number | null] | null)[]): IndexSpec[]