Expands the shape of an array by inserting a new dimension of size one at the position specified by dims. Parameters ---------- x : Array to expand. dim : Dimension name. New dimension will be stored in the axis position. axis : (Not recommended)
(
x: NamedArray[Any, _DType],
/,
*,
dim: _Dim | Default = _default,
axis: _Axis = 0,
)
| 146 | |
| 147 | # %% Manipulation functions |
| 148 | def expand_dims( |
| 149 | x: NamedArray[Any, _DType], |
| 150 | /, |
| 151 | *, |
| 152 | dim: _Dim | Default = _default, |
| 153 | axis: _Axis = 0, |
| 154 | ) -> NamedArray[Any, _DType]: |
| 155 | """ |
| 156 | Expands the shape of an array by inserting a new dimension of size one at the |
| 157 | position specified by dims. |
| 158 | |
| 159 | Parameters |
| 160 | ---------- |
| 161 | x : |
| 162 | Array to expand. |
| 163 | dim : |
| 164 | Dimension name. New dimension will be stored in the axis position. |
| 165 | axis : |
| 166 | (Not recommended) Axis position (zero-based). Default is 0. |
| 167 | |
| 168 | Returns |
| 169 | ------- |
| 170 | out : |
| 171 | An expanded output array having the same data type as x. |
| 172 | |
| 173 | Examples |
| 174 | -------- |
| 175 | >>> x = NamedArray(("x", "y"), np.asarray([[1.0, 2.0], [3.0, 4.0]])) |
| 176 | >>> expand_dims(x) |
| 177 | <xarray.NamedArray (dim_2: 1, x: 2, y: 2)> Size: 32B |
| 178 | array([[[1., 2.], |
| 179 | [3., 4.]]]) |
| 180 | >>> expand_dims(x, dim="z") |
| 181 | <xarray.NamedArray (z: 1, x: 2, y: 2)> Size: 32B |
| 182 | array([[[1., 2.], |
| 183 | [3., 4.]]]) |
| 184 | """ |
| 185 | xp = _get_data_namespace(x) |
| 186 | dims = x.dims |
| 187 | if dim is _default: |
| 188 | dim = f"dim_{len(dims)}" |
| 189 | d = list(dims) |
| 190 | d.insert(axis, dim) |
| 191 | out = x._new(dims=tuple(d), data=xp.expand_dims(x._data, axis=axis)) |
| 192 | return out |
| 193 | |
| 194 | |
| 195 | def permute_dims(x: NamedArray[Any, _DType], axes: _Axes) -> NamedArray[Any, _DType]: |
no test coverage detected
searching dependent graphs…