MCPcopy Create free account
hub / github.com/numpy/numpy / take_along_axis

Function take_along_axis

numpy/lib/shape_base.py:55–170  ·  view source on GitHub ↗

Take values from the input array by matching 1d index and data slices. This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths. Functions re

(arr, indices, axis)

Source from the content-addressed store, hash-verified

53
54@array_function_dispatch(_take_along_axis_dispatcher)
55def take_along_axis(arr, indices, axis):
56 """
57 Take values from the input array by matching 1d index and data slices.
58
59 This iterates over matching 1d slices oriented along the specified axis in
60 the index and data arrays, and uses the former to look up values in the
61 latter. These slices can be different lengths.
62
63 Functions returning an index along an axis, like `argsort` and
64 `argpartition`, produce suitable indices for this function.
65
66 .. versionadded:: 1.15.0
67
68 Parameters
69 ----------
70 arr : ndarray (Ni..., M, Nk...)
71 Source array
72 indices : ndarray (Ni..., J, Nk...)
73 Indices to take along each 1d slice of `arr`. This must match the
74 dimension of arr, but dimensions Ni and Nj only need to broadcast
75 against `arr`.
76 axis : int
77 The axis to take 1d slices along. If axis is None, the input array is
78 treated as if it had first been flattened to 1d, for consistency with
79 `sort` and `argsort`.
80
81 Returns
82 -------
83 out: ndarray (Ni..., J, Nk...)
84 The indexed result.
85
86 Notes
87 -----
88 This is equivalent to (but faster than) the following use of `ndindex` and
89 `s_`, which sets each of ``ii`` and ``kk`` to a tuple of indices::
90
91 Ni, M, Nk = a.shape[:axis], a.shape[axis], a.shape[axis+1:]
92 J = indices.shape[axis] # Need not equal M
93 out = np.empty(Ni + (J,) + Nk)
94
95 for ii in ndindex(Ni):
96 for kk in ndindex(Nk):
97 a_1d = a [ii + s_[:,] + kk]
98 indices_1d = indices[ii + s_[:,] + kk]
99 out_1d = out [ii + s_[:,] + kk]
100 for j in range(J):
101 out_1d[j] = a_1d[indices_1d[j]]
102
103 Equivalently, eliminating the inner loop, the last two lines would be::
104
105 out_1d[:] = a_1d[indices_1d]
106
107 See Also
108 --------
109 take : Take along an axis, using the same indices for every 1d slice
110 put_along_axis :
111 Put values into the destination array by matching 1d index and data slices
112

Callers 5

test_argequivalentMethod · 0.90
test_invalidMethod · 0.90
test_emptyMethod · 0.90
test_broadcastMethod · 0.90
test_broadcastMethod · 0.90

Calls 2

normalize_axis_indexFunction · 0.90
_make_along_axis_idxFunction · 0.85

Tested by 5

test_argequivalentMethod · 0.72
test_invalidMethod · 0.72
test_emptyMethod · 0.72
test_broadcastMethod · 0.72
test_broadcastMethod · 0.72