expand_dims¶
- expand_dims(x: array, /, axis: int | Tuple[int, ...]) array¶
Expands the shape of an array by inserting a new axis of size one at the position (or positions) specified by
axis.- Parameters:
x (array) – input array.
axis (Union[int, Tuple[int, ...]]) –
axis position(s) (zero-based). If
axisis an integer,axismust be equivalent to the tuple(axis,). Ifaxisis a tuple,a valid axis position must reside on the half-open interval
[-M, M), whereM = N + len(axis)andNis the number of dimensions inx.if the i-th entry is a negative integer, the axis position of the inserted singleton dimension in the output array must be computed as
M + axis[i].each entry of
axismust resolve to a unique positive axis position.for each entry of
axis, the corresponding dimension in the expanded output array must be a singleton dimension.for the remaining dimensions of the expanded output array, the output array dimensions must correspond to the dimensions of
xin order.if provided an invalid axis position, the function must raise an exception.
- Returns:
out (array) – an expanded output array. Must have the same data type as
x. Ifaxisis an integer, the output array must haveN + 1dimensions. Ifaxisis a tuple, the output array must haveN + len(axis)dimensions.- Raises:
IndexError – If provided an invalid
axis, anIndexErrorshould be raised.
Notes
Calling this function with a tuple of axis positions must be semantically equivalent to calling this function repeatedly with a single axis position only when the following three conditions are met:
each entry of the tuple is normalized to positive axis positions according to the number of dimensions in the expanded output array.
the normalized positive axis positions are sorted in ascending order.
the normalized positive axis positions are unique.
Changed in version 2025.12: Added support for specifying a tuple of axis positions.