def swap_axis_to_0(x, axis):
"""Insert a new singleton axis at position 0 and swap it with the
specified axis. The resulting array has an additional dimension,
with ``axis`` + 1 (which was ``axis`` before the insertion of the
new axis) of ``x`` at position 0, and a singleton axis at position
``axis`` + 1.
Parameters
----------
x : ndarray
Input array
axis : int
Index of axis in ``x`` to swap to axis index 0.
Returns
-------
arr : ndarray
Output array
"""
return np.ascontiguousarray(np.swapaxes(x[np.newaxis, ...], 0, axis+1))
评论列表
文章目录