np.take: What is Numpy take() Function in Python

The np.take() is a numpy library function that returns elements from the array along the mentioned axis and indices. This means we can get elements of an array by its indices, and if the axis is mentioned, then all elements present at that index will be printed axis-wise.

The numpy take() function takes elements from an array along an axis. The take() function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need items along the given axis.

Syntax

numpy.take(array, indices, axis = None, out = None, mode =’raise’)

Parameters

The np.take() function can take up to five parameters:

  1. array: This is the array on which we will work.
  2. indices: These are the indices of the values to be collected.
  3. axis: This field is optional. This is the axis over which we have to fetch the elements. Its default value is ‘None’; the array is flattened in this case.
  4. mode: There are three types of mode that work on how out-of-bound will work:
    1. raise: raise an error in case of out-of-bound
    2. warp: warp around
    3. clip: clip to the range

        Remember, this field is optional.

        Return Value

        The take() function returns an nD-array that has the same type.

        Example 1

        import numpy as np
        
        arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
        
        #Printing the array
        print("The array is: ")
        print(arr)
        
        #Printing values without mentioning axis
        indices = [2, 7]
        print("Values at position 2 and 7 are: ", np.take(arr, indices))
        
        #Printing valoues with axis
        indices = [0, 2]
        print("Values at position 0 and 2 are: \n", np.take(arr, indices, axis=1))

        Output

        The array is:
        [[ 1 2 3]
         [ 4 5 6]
         [ 7 8 9]
         [50 51 52]]
        Values at position 2 and 7 are: [3 8]
        Values at position 0 and 2 are:
         [[ 1 3]
         [ 4 6]
         [ 7 9]
         [50 52]]

        Explanation

        In this example, we have first declared an array of shapes 3×4 and printed that array. 

        Then we wanted to print values by giving its indices with the help of take().

        At first, we called take() without mentioning the axis, and we can see that in indices, we have given value [2,7]. As the axis is not mentioned, the array was flattened, and the values at indices 2 and 7 were printed.

        In the second case, we have mentioned axis=1, which will print values column-wise and indices =[0,2].

        We can see that we got a result as a 2×4 matrix. take() has printed values of the column, which has indices 0 and 2, respectively.

        Example 2

        import numpy as np
        
        arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
        
        #Printing the array
        print("The array is: ")
        print(arr)
        
        #Printing values mode=warp
        indices = [0, 6]
        print("Values at position 0 and 6 are [mode=warp]:\n ",
         np.take(arr, indices, axis=1, mode='warp'))
        
        #Printing values mode=clip
        indices = [1, 5]
        print("Values at position 1 and 5 are [mode=clip]:\n ",
         np.take(arr, indices, axis=1, mode='clip'))
        
        #Printing values mode=raise
        indices = [2, 7]
        print("Values at position 2 and 7 are [mode=raise]:\n ", np.take(arr, indices, axis=1, mode='raise'))

        Output

        The array is:
        [[ 1 2 3]
         [ 4 5 6]
         [ 7 8 9]
         [50 51 52]]
        Values at position 0 and 6 are [mode=warp]:
         [[ 1 1]
         [ 4 4]
         [ 7 7]
         [50 50]]
        Values at position 1 and 5 are [mode=clip]:
         [[ 2 3]
         [ 5 6]
         [ 8 9]
         [51 52]]
        Traceback (most recent call last):
         File "take2.py", line 21, in <module>
         print("Values at position 2 and 7 are [mode=raise]:\n ",np.take(arr,indices,axis=1,mode='raise'))
         File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 189, in take
         return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
         File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 56, in _wrapfunc
         return getattr(obj, method)(*args, **kwds)
        IndexError: index 7 is out of bounds for size 3

        Explanation

        In this example, we have declared an array of shapes 3×4 and printed that array.

        In the first case, we can see that we have given values of indices [0,6] and axis =1; we don’t have such an axis with an index of 6.

        When we called take(), we passed mode=’warp’; as we tried to get the index out-of-bound, the ‘warp’ mode warped the array and printed values without any error.

        In the second case, we gave mode=’clip’; we can see that the array was clipped and values were printed.

        At last, we have given mode=’raise’, as this mode raises an error in the case of out of bound, we got an error in the output.

        That’s it for the np.take() function.

        Leave a Comment

        This site uses Akismet to reduce spam. Learn how your comment data is processed.