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

Function put_along_axis

numpy/lib/shape_base.py:178–260  ·  view source on GitHub ↗

Put values into the destination 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 place values into the latter. These slices can be different lengths. Functio

(arr, indices, values, axis)

Source from the content-addressed store, hash-verified

176
177@array_function_dispatch(_put_along_axis_dispatcher)
178def put_along_axis(arr, indices, values, axis):
179 """
180 Put values into the destination array by matching 1d index and data slices.
181
182 This iterates over matching 1d slices oriented along the specified axis in
183 the index and data arrays, and uses the former to place values into the
184 latter. These slices can be different lengths.
185
186 Functions returning an index along an axis, like `argsort` and
187 `argpartition`, produce suitable indices for this function.
188
189 .. versionadded:: 1.15.0
190
191 Parameters
192 ----------
193 arr : ndarray (Ni..., M, Nk...)
194 Destination array.
195 indices : ndarray (Ni..., J, Nk...)
196 Indices to change along each 1d slice of `arr`. This must match the
197 dimension of arr, but dimensions in Ni and Nj may be 1 to broadcast
198 against `arr`.
199 values : array_like (Ni..., J, Nk...)
200 values to insert at those indices. Its shape and dimension are
201 broadcast to match that of `indices`.
202 axis : int
203 The axis to take 1d slices along. If axis is None, the destination
204 array is treated as if a flattened 1d view had been created of it.
205
206 Notes
207 -----
208 This is equivalent to (but faster than) the following use of `ndindex` and
209 `s_`, which sets each of ``ii`` and ``kk`` to a tuple of indices::
210
211 Ni, M, Nk = a.shape[:axis], a.shape[axis], a.shape[axis+1:]
212 J = indices.shape[axis] # Need not equal M
213
214 for ii in ndindex(Ni):
215 for kk in ndindex(Nk):
216 a_1d = a [ii + s_[:,] + kk]
217 indices_1d = indices[ii + s_[:,] + kk]
218 values_1d = values [ii + s_[:,] + kk]
219 for j in range(J):
220 a_1d[indices_1d[j]] = values_1d[j]
221
222 Equivalently, eliminating the inner loop, the last two lines would be::
223
224 a_1d[indices_1d] = values_1d
225
226 See Also
227 --------
228 take_along_axis :
229 Take values from the input array by matching 1d index and data slices
230
231 Examples
232 --------
233
234 For this sample array
235

Callers 2

test_replace_maxMethod · 0.90
test_broadcastMethod · 0.90

Calls 2

normalize_axis_indexFunction · 0.90
_make_along_axis_idxFunction · 0.85

Tested by 2

test_replace_maxMethod · 0.72
test_broadcastMethod · 0.72