mindspore.numpy.apply_along_axis

View Source On Gitee
mindspore.numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)[source]

Applies a function to 1-D slices along the given axis. Executes func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.

Parameters
  • func1d (function) – Maps (M,) -> (Nj…). This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.

  • axis (int) – Axis along which arr is sliced.

  • arr (Tensor) – Input array with shape (Ni…, M, Nk…).

  • args (any) – Additional arguments to func1d.

  • kwargs (any) – Additional named arguments to func1d.

Returns

Tensor with shape (Ni…, Nj…, Nk…), the output array. Its shape is identical to the shape of arr, except along the axis dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar, the output will have one fewer dimensions than arr.

Supported Platforms:

Ascend GPU CPU

Raises

ValueError – If axis is out of the range.

Examples

>>> import mindspore.numpy as np
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> print(np.apply_along_axis(np.diag, -1, b))
[[[1 0 0]
[0 2 0]
[0 0 3]]
[[4 0 0]
[0 5 0]
[0 0 6]]
[[7 0 0]
[0 8 0]
[0 0 9]]]