As it was not mentioned clearly enough (and i was looking for it too):
an equivalent to:
a = my_array[:, :, :, 8]
b = my_array[:, :, :, 2:7]
is:
a = my_array.take(indices=8, axis=3)
b = my_array.take(indices=range(2, 7), axis=3)
Answer from Śmigło on Stack OverflowNumPy
numpy.org › devdocs › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.6.dev0 Manual
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.
Top answer 1 of 7
78
As it was not mentioned clearly enough (and i was looking for it too):
an equivalent to:
a = my_array[:, :, :, 8]
b = my_array[:, :, :, 2:7]
is:
a = my_array.take(indices=8, axis=3)
b = my_array.take(indices=range(2, 7), axis=3)
2 of 7
44
I think one way would be to use slice(None):
>>> m = np.arange(2*3*5).reshape((2,3,5))
>>> axis, start, end = 2, 1, 3
>>> target = m[:, :, 1:3]
>>> target
array([[[ 1, 2],
[ 6, 7],
[11, 12]],
[[16, 17],
[21, 22],
[26, 27]]])
>>> slc = [slice(None)] * len(m.shape)
>>> slc[axis] = slice(start, end)
>>> np.allclose(m[slc], target)
True
I have a vague feeling I've used a function for this before, but I can't seem to find it now..
NumPy
numpy.org › doc › stable › reference › generated › numpy.apply_along_axis.html
numpy.apply_along_axis — NumPy v2.5 Manual
Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk]) ... This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
NumPy
numpy.org › doc › stable › reference › generated › numpy.put_along_axis.html
numpy.put_along_axis — NumPy v1.26 Manual
January 31, 2021 - 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.
IncludeHelp
includehelp.com › python › how-to-slice-a-numpy-array-along-a-dynamically-specified-axis.aspx
Python - How to slice a numpy array along a dynamically specified axis?
By this, we mean that we will specify the axis, starting point, and ending point dynamically and then slice the array. For this purpose, we will use numpy.arr.take() method which takes indices and axis as a parameter that can be assigned in order to slice the array dynamically.
NumPy
numpy.org › doc › stable › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.4 Manual
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.
NumPy
numpy.org › doc › 1.20 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v1.20 Manual
numpy.take_along_axis(arr, indices, axis)[source]¶ · Take values from the input array by matching 1d index and data slices.
NumPy
numpy.org › devdocs › reference › generated › numpy.put_along_axis.html
numpy.put_along_axis — NumPy v2.5.dev0 Manual
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.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.apply_along_axis.html
numpy.apply_along_axis — NumPy v2.0 Manual
Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk]) ... This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
NumPy
numpy.org › doc › stable › reference › generated › numpy.take.html
numpy.take — NumPy v2.5 Manual
‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers. ... The returned array has the same type as a. ... By eliminating the inner loop in the description above, and using s_ to build simple slice objects, take can be expressed in terms of applying fancy indexing to each 1-d slice:
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.2 Manual
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.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.0 Manual
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.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.1 Manual
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.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take.html
numpy.take — NumPy v2.2 Manual
‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers. ... The returned array has the same type as a. ... By eliminating the inner loop in the description above, and using s_ to build simple slice objects, take can be expressed in terms of applying fancy indexing to each 1-d slice:
Stack Overflow
stackoverflow.com › questions › 55344621 › how-to-slice-from-a-multi-dimensional-array-along-an-axis-in-numpy
python - How to slice from a multi-dimensional array along an axis in NumPy? - Stack Overflow
That is, indices has the same shape as x except for possibly the first axis. Assume that every entry of indices is an int between 0 and n - 1. I want to create an array y that has the same shape as indices and that satisfies ... for each i between 0 and n - 1. Here ... signifies an arbitrary combination of indices for the remaining axes, not an Ellipsis object. For example, here is how I could create y if x is three-dimensional using for-loops: import numpy as np x = np.arange(24).reshape((4, 2, 3)) print('x =', x, sep='\n') indices = np.asarray([[[1, 0, 1], [2, 1, 2]], [[3, 1, 2], [0, 0, 1]]]) print('indices =', indices, sep='\n') y = np.empty(indices.shape, dtype=x.dtype) for i in range(indices.shape[0]): for j in range(indices.shape[1]): for k in range(indices.shape[2]): y[i, j, k] = x[indices[i, j, k], j, k] # Defining property of y print('y =', y, sep='\n')
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.apply_along_axis.html
numpy.apply_along_axis — NumPy v2.2 Manual
Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk]) ... This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.apply_along_axis.html
numpy.apply_along_axis — NumPy v2.1 Manual
Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk]) ... This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.3 Manual
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.
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › BasicIndexing.html
Introducing Basic and Advanced Indexing — Python Like You Mean It
Given a \(N\)-dimensional array, x, index into x such that you axis entry-0 of axis-0, the last entry of axis-\((N-1)\), slicing along all intermediate dimensions. \(N\) is at least \(2\). Using an Ellipsis object in the index allows us to signal NumPy to insert the slices along the \(N - 2\) intermediate axis of x:
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.put_along_axis.html
numpy.put_along_axis — NumPy v2.2 Manual
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.