take
- mdtools.numpy_helper_functions.take(a, start=None, stop=None, step=None, axis=None)[source]
Take a slice from an array along an axis.
For example,
take(a, axis=2, start, stop, step)
is equivalent toa[:,:,start:stop:step,...]
.- Parameters:
- Returns:
a_slice (
numpy.ndarray
) – A view of the requested slice along the requested axis.
See also
numpy.take()
Similar function but supports fancy indexing and therefore always returns a copy instead of a view.
Notes
This function is similar to
numpy.take()
. The difference is that this function only supports slicing and not fancy indexing but therefore always returns a view instead of a copy.References
Inspired by Leland Hepworth (https://stackoverflow.com/a/64436208)
Examples
>>> a = np.arange(6).reshape(2,3) >>> x = mdt.nph.take(a) >>> y = np.take(a, indices=np.arange(a.size)) >>> np.array_equal(x, y) True >>> x array([0, 1, 2, 3, 4, 5]) >>> x[0] = 6 # x is a view of a, i.e. changing x changes a. >>> x array([6, 1, 2, 3, 4, 5]) >>> a array([[6, 1, 2], [3, 4, 5]]) >>> y[0] = 7 # y is a copy of a, i.e. changing y does not affect a. >>> y array([7, 1, 2, 3, 4, 5]) >>> a array([[6, 1, 2], [3, 4, 5]]) >>> x = mdt.nph.take(a, axis=0) >>> y = np.take(a, indices=np.arange(a.shape[0]), axis=0) >>> np.array_equal(x, y) True >>> x array([[6, 1, 2], [3, 4, 5]])
>>> a = np.arange(24).reshape(2,3,4) >>> mdt.nph.take(a, axis=0) array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> mdt.nph.take(a, start=1, axis=1) array([[[ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[16, 17, 18, 19], [20, 21, 22, 23]]]) >>> mdt.nph.take(a, stop=3, axis=2) array([[[ 0, 1, 2], [ 4, 5, 6], [ 8, 9, 10]], [[12, 13, 14], [16, 17, 18], [20, 21, 22]]]) >>> np.array_equal( ... mdt.nph.take(a, stop=3, axis=2), ... mdt.nph.take(a, stop=-1, axis=2) ... ) True >>> np.array_equal( ... mdt.nph.take(a, stop=3, axis=2), ... mdt.nph.take(a, stop=3, axis=-1) ... ) True >>> np.array_equal( ... mdt.nph.take(a, axis=2, stop=3), ... np.take(a, indices=np.arange(a.shape[2] - 1), axis=2) ... ) True >>> mdt.nph.take(a, step=2, axis=2) array([[[ 0, 2], [ 4, 6], [ 8, 10]], [[12, 14], [16, 18], [20, 22]]])