MCPcopy
hub / github.com/numpy/numpy / apply_along_axis

Function apply_along_axis

numpy/ma/extras.py:350–429  ·  view source on GitHub ↗

(This docstring should be overwritten)

(func1d, axis, arr, *args, **kwargs)

Source from the content-addressed store, hash-verified

348
349
350def apply_along_axis(func1d, axis, arr, *args, **kwargs):
351 """
352 (This docstring should be overwritten)
353 """
354 arr = array(arr, copy=False, subok=True)
355 nd = arr.ndim
356 axis = normalize_axis_index(axis, nd)
357 ind = [0] * (nd - 1)
358 i = np.zeros(nd, 'O')
359 indlist = list(range(nd))
360 indlist.remove(axis)
361 i[axis] = slice(None, None)
362 outshape = np.asarray(arr.shape).take(indlist)
363 i.put(indlist, ind)
364 res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
365 # if res is a number, then we have a smaller output array
366 asscalar = np.isscalar(res)
367 if not asscalar:
368 try:
369 len(res)
370 except TypeError:
371 asscalar = True
372 # Note: we shouldn't set the dtype of the output from the first result
373 # so we force the type to object, and build a list of dtypes. We'll
374 # just take the largest, to avoid some downcasting
375 dtypes = []
376 if asscalar:
377 dtypes.append(np.asarray(res).dtype)
378 outarr = zeros(outshape, object)
379 outarr[tuple(ind)] = res
380 Ntot = np.prod(outshape)
381 k = 1
382 while k < Ntot:
383 # increment the index
384 ind[-1] += 1
385 n = -1
386 while (ind[n] >= outshape[n]) and (n > (1 - nd)):
387 ind[n - 1] += 1
388 ind[n] = 0
389 n -= 1
390 i.put(indlist, ind)
391 res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
392 outarr[tuple(ind)] = res
393 dtypes.append(asarray(res).dtype)
394 k += 1
395 else:
396 res = array(res, copy=False, subok=True)
397 j = i.copy()
398 j[axis] = ([slice(None, None)] * res.ndim)
399 j.put(indlist, ind)
400 Ntot = np.prod(outshape)
401 holdshape = outshape
402 outshape = list(arr.shape)
403 outshape[axis] = res.shape
404 dtypes.append(asarray(res).dtype)
405 outshape = flatten_inplace(outshape)
406 outarr = zeros(outshape, object)
407 outarr[tuple(flatten_inplace(j.tolist()))] = res

Callers 2

test_3dMethod · 0.90
test_3d_kwargsMethod · 0.90

Calls 12

sliceFunction · 0.85
zerosFunction · 0.85
flatten_inplaceFunction · 0.85
takeMethod · 0.80
putMethod · 0.80
arrayFunction · 0.70
asarrayFunction · 0.70
tolistMethod · 0.45
prodMethod · 0.45
copyMethod · 0.45
dtypeMethod · 0.45
maxMethod · 0.45

Tested by 2

test_3dMethod · 0.72
test_3d_kwargsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…