take_along_axis#
- ivy.take_along_axis(arr, indices, axis, /, *, mode='fill', out=None)[source]#
Take values from the input array by matching 1d index and data slices.
- Parameters:
arr (
Union
[Array
,NativeArray
]) – The source array.indices (
Union
[Array
,NativeArray
]) – The indices of the values to extract.axis (
int
) – The axis over which to select values. If axis is None, arr is treated as a flattened 1D array.mode (
str
) – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds indices (default:'fill'
) will be handled.out (
Optional
[Array
]) – The output array. (default:None
)
- Return type:
- Returns:
ret – The returned array has the same shape as indices.
Examples
>>> arr = ivy.array([[4, 3, 5], [1, 2, 1]]) >>> indices = ivy.array([[0, 1, 1], [2, 0, 0]]) >>> y = ivy.take_along_axis(arr, indices, 1) >>> print(y) ivy.array([[4, 3, 3], [1, 1, 1]])
- Array.take_along_axis(self, indices, axis, /, *, mode='fill', out=None)#
ivy.Array instance method variant of ivy.take_along_axis. This method simply wraps the function, and so the docstring for ivy.take_along_axis also applies to this method with minimal changes.
- Parameters:
self (
Array
) – The source array.indices (
Array
) – The indices of the values to extract.axis (
int
) – The axis over which to select values.mode (
str
) – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds (default:'fill'
) indices will be handled.out (
Optional
[Array
]) – Optional output, for writing the result to. (default:None
)
- Return type:
Array
- Returns:
ret – The returned array has the same shape as indices.
Examples
>>> arr = ivy.array([[4, 3, 5], [1, 2, 1]]) >>> indices = ivy.array([[0, 1, 1], [2, 0, 0]]) >>> y = arr.take_along_axis(indices, 1) >>> print(y) ivy.array([[4, 3, 3], [1, 1, 1]])
- Container.take_along_axis(self, indices, axis, mode='fill', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#
ivy.Container instance method variant of ivy.take_along_axis. This method simply wraps the function, and so the docstring for ivy.take_along_axis also applies to this method with minimal changes.
- Parameters:
self (
Union
[Container
,Array
,NativeArray
]) – container with array inputs.indices (
Union
[Container
,Array
,NativeArray
]) – container with indices of the values to extract.axis (
int
) – The axis over which to select values. If axis is None, then arr and indices must be 1-D sequences of the same length.mode (
str
) – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds (default:'fill'
) indices will be handled.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
]]]) – The keychains to apply or not apply the method to. Default isNone
. (default:None
)to_apply (
bool
) – If True, the method will be applied to key_chains, otherwise key_chains (default:True
) will be skipped. Default isTrue
.prune_unapplied (
bool
) – Whether to prune key_chains for which the function was not applied. (default:False
) Default isFalse
.map_sequences (
bool
) – Whether to also map method to sequences (lists, tuples). (default:False
) Default isFalse
.out (
Optional
[Container
]) – optional output container, for writing the result to. (default:None
)
- Return type:
Container
- Returns:
ret – a container with arrays of the same shape as those in indices.
Examples
>>> arr = ivy.Container(a=ivy.array([[1, 2], [3, 4]]), b=ivy.array([[5, 6], [7, 8]])) >>> indices = ivy.Container(a=ivy.array([[0, 0], [1, 1]]), b=ivy.array([[1, 0], [1, 0]])) >>> arr.take_along_axis(indices, axis=1) [{ a: ivy.array([[1, 1], [4, 4]]), b: ivy.array([[6, 5], [8, 7]]) }]