MCPcopy Index your code
hub / github.com/numpy/numpy / put_along_axis

Function put_along_axis

numpy/lib/_shape_base_impl.py:187–269  ·  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

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

Callers 3

test_replace_maxMethod · 0.90
test_broadcastMethod · 0.90
test_invalidMethod · 0.90

Calls 1

_make_along_axis_idxFunction · 0.85

Tested by 3

test_replace_maxMethod · 0.72
test_broadcastMethod · 0.72
test_invalidMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…