Skip to main content

NDArray Class

numpy-ts provides two array classes:
  • NDArray — The full class with 100+ chainable instance methods. Returned by the default numpy-ts entry point.
  • NDArrayCore — The minimal class with only core properties and element access. Returned by the tree-shakeable numpy-ts/core entry point.
NDArray extends NDArrayCore. Every property and core method documented on this page exists on both classes.
import * as np from 'numpy-ts';

const a = np.array([[1, 2, 3], [4, 5, 6]]);
console.log(a.shape);   // [2, 3]
console.log(a.dtype);   // 'float64'
console.log(a.ndim);    // 2
console.log(a.size);    // 6

Properties

shape

get shape(): readonly number[]
Tuple of array dimensions. A 2D array with 3 rows and 4 columns has shape [3, 4].
PropertyTypeDescription
shapereadonly number[]Dimension sizes, one entry per axis.
const a = np.zeros([3, 4, 5]);
console.log(a.shape); // [3, 4, 5]

ndim

get ndim(): number
Number of array dimensions (axes). Equivalent to shape.length.
PropertyTypeDescription
ndimnumberNumber of dimensions.
const a = np.zeros([3, 4, 5]);
console.log(a.ndim); // 3

size

get size(): number
Total number of elements in the array. Equal to the product of all shape dimensions.
PropertyTypeDescription
sizenumberTotal element count.
const a = np.zeros([3, 4]);
console.log(a.size); // 12

dtype

get dtype(): DType
Data type of the array elements. One of: 'float64', 'float32', 'complex128', 'complex64', 'int64', 'int32', 'int16', 'int8', 'uint64', 'uint32', 'uint16', 'uint8', 'bool'.
PropertyTypeDescription
dtypeDTypeElement data type string.
const a = np.array([1, 2, 3]);
console.log(a.dtype); // 'float64'

const b = np.zeros([2, 2], 'int32');
console.log(b.dtype); // 'int32'

data

get data(): TypedArray
The underlying JavaScript TypedArray buffer holding the raw element data.
PropertyTypeDescription
dataTypedArrayThe backing typed array (Float64Array, Int32Array, etc.).
TypedArray is a union of Float64Array | Float32Array | BigInt64Array | Int32Array | Int16Array | Int8Array | BigUint64Array | Uint32Array | Uint16Array | Uint8Array.
const a = np.array([10, 20, 30]);
console.log(a.data); // Float64Array([10, 20, 30])

strides

get strides(): readonly number[]
Stride for each dimension, measured in elements (not bytes). Used internally to map multi-dimensional indices to flat buffer positions.
PropertyTypeDescription
stridesreadonly number[]Elements to skip for each axis.
const a = np.zeros([3, 4]);
console.log(a.strides); // [4, 1]

offset

get offset(): number
Offset into the underlying data buffer, in elements. Nonzero for views created by slicing.
PropertyTypeDescription
offsetnumberElement offset into data.
const a = np.arange(10);
const b = a.slice('3:7');
console.log(b.offset); // 3
The offset property is accessed via the internal storage object (a.storage.offset). On NDArrayCore it is not directly exposed as a top-level property but can be read from the storage.

base

get base(): NDArray | null
If this array is a view of another array, base references the original array that owns the data. Returns null if the array owns its own data.
PropertyTypeDescription
baseNDArray | nullThe owning array, or null.
const a = np.arange(10);
const b = a.slice('2:8');
console.log(b.base === a);   // true
console.log(a.base === null); // true

flags

get flags(): {
  C_CONTIGUOUS: boolean;
  F_CONTIGUOUS: boolean;
  OWNDATA: boolean;
}
Information about the memory layout and ownership of the array.
FlagTypeDescription
C_CONTIGUOUSbooleantrue if data is stored in C (row-major) order with no gaps.
F_CONTIGUOUSbooleantrue if data is stored in Fortran (column-major) order with no gaps.
OWNDATAbooleantrue if this array owns its data buffer (not a view).
const a = np.array([[1, 2], [3, 4]]);
console.log(a.flags);
// { C_CONTIGUOUS: true, F_CONTIGUOUS: false, OWNDATA: true }

T

get T(): NDArray
Transpose of the array. Returns a view with axes reversed. Equivalent to calling transpose() with no arguments.
PropertyTypeDescription
TNDArrayTransposed view.
T is a property (no parentheses needed). It is only available on NDArray, not NDArrayCore.
const a = np.array([[1, 2, 3], [4, 5, 6]]);
console.log(a.T.shape); // [3, 2]

itemsize

get itemsize(): number
Size of a single array element in bytes.
PropertyTypeDescription
itemsizenumberBytes per element.
const a = np.zeros([3], 'float64');
console.log(a.itemsize); // 8

const b = np.zeros([3], 'int8');
console.log(b.itemsize); // 1

nbytes

get nbytes(): number
Total number of bytes consumed by the array elements. Equal to size * itemsize.
PropertyTypeDescription
nbytesnumberTotal byte count.
const a = np.zeros([100, 100], 'float32');
console.log(a.nbytes); // 40000

Core methods

copy

copy(): NDArray
Return a deep copy of the array. The returned array owns its own data and is not a view.
ParameterTypeDefaultDescription
(none)
Returns: NDArray — A new array with copied data.
const a = np.array([1, 2, 3]);
const b = a.copy();
b.set([0], 99);
console.log(a.get([0])); // 1  (original unchanged)

astype

astype(dtype: DType, copy?: boolean): NDArray
Cast the array to a specified data type.
ParameterTypeDefaultDescription
dtypeDTypeTarget data type.
copybooleantrueIf false and dtype already matches, return this instead of a copy.
Returns: NDArray — Array with the requested dtype.
const a = np.array([1.7, 2.3, 3.9]);
const b = a.astype('int32');
console.log(b.toArray()); // [1, 2, 3]
console.log(b.dtype);     // 'int32'

fill

fill(value: number | bigint): void
Fill the array in-place with a scalar value.
ParameterTypeDefaultDescription
valuenumber | bigintValue to fill with.
Returns: void
const a = np.zeros([2, 3]);
a.fill(7);
console.log(a.toArray()); // [[7, 7, 7], [7, 7, 7]]

toArray

toArray(): NestedArray
Convert the array to a nested JavaScript array. Scalar (0-d) arrays return a single number.
ParameterTypeDefaultDescription
(none)
Returns: number | number[] | number[][] | ... — Nested JavaScript array matching the shape.
const a = np.array([[1, 2], [3, 4]]);
console.log(a.toArray()); // [[1, 2], [3, 4]]

tolist

tolist(): NestedArray
Return the array as a nested list. Identical to toArray().
ParameterTypeDefaultDescription
(none)
Returns: number | number[] | number[][] | ... — Same as toArray().

tobytes

tobytes(): ArrayBuffer
Return the raw bytes of the array data as an ArrayBuffer. If the array is not C-contiguous, a contiguous copy is made first.
ParameterTypeDefaultDescription
(none)
Returns: ArrayBuffer — Raw byte representation.
const a = np.array([1, 2, 3], 'int32');
const bytes = a.tobytes();
console.log(bytes.byteLength); // 12  (3 elements * 4 bytes)

get

get(indices: number[]): number | bigint | Complex
Get a single element at the given multi-dimensional indices. Supports negative indexing.
ParameterTypeDefaultDescription
indicesnumber[]One index per dimension. Negative values count from the end.
Returns: number | bigint | Complex — The scalar element value.
const a = np.array([[10, 20], [30, 40]]);
console.log(a.get([0, 1])); // 20
console.log(a.get([-1, -1])); // 40

set

set(indices: number[], value: number | bigint | Complex): void
Set a single element at the given multi-dimensional indices. Supports negative indexing.
ParameterTypeDefaultDescription
indicesnumber[]One index per dimension.
valuenumber | bigint | ComplexValue to set.
Returns: void
const a = np.zeros([2, 2]);
a.set([0, 1], 42);
console.log(a.get([0, 1])); // 42

slice

slice(...sliceStrs: string[]): NDArray
Slice the array using NumPy-style string syntax. Returns a view (shares data with the original).
ParameterTypeDefaultDescription
...sliceStrsstring[]Slice specifications, one per dimension. Uses NumPy syntax: 'start:stop:step', ':', 'i' (integer index).
Returns: NDArray — A view into the original array.
const a = np.arange(10);

// Elements 2 through 6
const b = a.slice('2:7');
console.log(b.toArray()); // [2, 3, 4, 5, 6]

// Every other element
const c = a.slice('::2');
console.log(c.toArray()); // [0, 2, 4, 6, 8]

// 2D slicing
const m = np.arange(12).reshape(3, 4);
const sub = m.slice('0:2', '1:3');
console.log(sub.toArray()); // [[1, 2], [5, 6]]

row

row(i: number): NDArray
Get a single row from a 2D+ array. Convenience method for slice(String(i), ':').
ParameterTypeDefaultDescription
inumberRow index.
Returns: NDArray — The selected row as a view.
const a = np.array([[1, 2, 3], [4, 5, 6]]);
console.log(a.row(1).toArray()); // [4, 5, 6]

col

col(j: number): NDArray
Get a single column from a 2D+ array. Convenience method for slice(':', String(j)).
ParameterTypeDefaultDescription
jnumberColumn index.
Returns: NDArray — The selected column as a view.
const a = np.array([[1, 2, 3], [4, 5, 6]]);
console.log(a.col(0).toArray()); // [1, 4]

rows

rows(start: number, stop: number): NDArray
Get a range of rows from a 2D+ array.
ParameterTypeDefaultDescription
startnumberStart row index (inclusive).
stopnumberStop row index (exclusive).
Returns: NDArray — View containing the selected rows.
const a = np.arange(12).reshape(4, 3);
console.log(a.rows(1, 3).toArray()); // [[3, 4, 5], [6, 7, 8]]

cols

cols(start: number, stop: number): NDArray
Get a range of columns from a 2D+ array.
ParameterTypeDefaultDescription
startnumberStart column index (inclusive).
stopnumberStop column index (exclusive).
Returns: NDArray — View containing the selected columns.
const a = np.arange(12).reshape(3, 4);
console.log(a.cols(1, 3).toArray()); // [[1, 2], [5, 6], [9, 10]]

item

item(...args: number[]): number | bigint | Complex
Extract a scalar value from the array. With no arguments, works on arrays with exactly one element. With one argument, treats it as a flat index. With multiple arguments, treats them as multi-dimensional indices.
ParameterTypeDefaultDescription
...argsnumber[]No args (single-element arrays), one flat index, or multi-dimensional indices.
Returns: number | bigint | Complex — The scalar value.
const a = np.array([[10, 20], [30, 40]]);
console.log(a.item(0, 1)); // 20
console.log(a.item(3));    // 40 (flat index)

const scalar = np.array([42]);
console.log(scalar.item()); // 42

Static methods

NDArray.fromStorage

static fromStorage(storage: ArrayStorage, base?: NDArray): NDArray
Create an NDArray from an internal ArrayStorage object. This is primarily used internally by numpy-ts operations.
ParameterTypeDefaultDescription
storageArrayStorageThe storage backing the array.
baseNDArrayundefinedIf provided, marks this array as a view of base.
Returns: NDArray — A new NDArray wrapping the given storage.
fromStorage is an internal API. Prefer np.array(), np.zeros(), and other creation functions for normal usage.