Caution

You're reading the documentation for a development version. For the latest released version, please have a look at 0.10.2.

librosa.util.expand_to

librosa.util.expand_to(x, *, ndim, axes)[source]

Expand the dimensions of an input array with

Parameters:
xnp.ndarray

The input array

ndimint

The number of dimensions to expand to. Must be at least x.ndim

axesint or slice

The target axis or axes to preserve from x. All other axes will have length 1.

Returns:
x_expnp.ndarray
The expanded version of x, satisfying the following:

x_exp[axes] == x x_exp.ndim == ndim

See also

np.expand_dims

Examples

Expand a 1d array into an (n, 1) shape

>>> x = np.arange(3)
>>> librosa.util.expand_to(x, ndim=2, axes=0)
array([[0],
   [1],
   [2]])

Expand a 1d array into a (1, n) shape

>>> librosa.util.expand_to(x, ndim=2, axes=1)
array([[0, 1, 2]])

Expand a 2d array into (1, n, m, 1) shape

>>> x = np.vander(np.arange(3))
>>> librosa.util.expand_to(x, ndim=4, axes=[1,2]).shape
(1, 3, 3, 1)