Built with Tutify.io

    AIStudyLabs

    NumPy flip() Function in Python

    NumPy flip() Function in Python

    In NumPy, the flip() function is used to reverse the order of array elements along a specified axis. The shape of the array is preserved, but the elements are reordered. In this article, I will explain the NumPy flip() function using this how to return a reversed array with shape preserved with examples.

    <a id="h-1-quick-examples-of-flip-function"></a>

    1. Quick Examples of flip() Function

    If you are in a hurry, below are some quick examples of the NumPy flip() function in Python.

    
    # Quick examples of flip() function 
    
    # Example 1: Use numpy flip() function on 1-d array
    arr = np.array([2, 4, 6, 8, 10])
    arr2 = np.flip(arr)
    
    # Example 2: Use numpy flip() function with 2-D arrays
    arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
    arr2 = np.flip(arr)
    
    # Example 3: Use numpy.flip() function
    # To reverse the order along columns
    arr2 = np.flip(arr, axis=1)
    
    # Example 4: Use numpy.flip() function
    # To reverse the order along rows 
    arr2 = np.flip(arr, axis=0)
    
    # Example 5: Use slicing with flip() 
    arr2 = np.flip(arr[:,1])
    

    <a id="h-2-syntax-of-numpy-flip"></a>

    2. Syntax of NumPy flip()

    Following is the syntax of the numpy.flip() function.

    
    # Syntax of flip()
    numpy.flip(arr, axis=None)
    

    <a id="h-2-1-parameters-of-flip"></a>

    2.1 Parameters of flip()

    Following are the parameters of the flip() function.

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    It returns reversed array while preserving its shape.

    <a id="h-3-use-numpy-flip-function-on-1-d-array"></a>

    3 Use NumPy flip() Function on 1-D Array

    Let's create a single-dimensional array using array() function and then apply the NumPy flip() function to reverse the array without changing its shape.

    The below example, arr is a simple 1-D array [2, 4, 6, 8, 10]. The numpy.flip() function is then applied to reverse the order of elements in the array. The original and flipped arrays are then printed.

    
    # Import numpy module
    import numpy as np
    
    # Create an 1D input array  
    arr = np.array([2, 4, 6, 8, 10])
    print("Original array:", arr)
    
    # Use numpy flip() function on 1-d array
    arr2 = np.flip(arr)
    print("Flipped 1D array:", arr2)
    

    Yields below output.

    <a id="h-4-use-numpy-flip-function-with-2-d-arrays"></a>

    4. Use NumPy flip() Function with 2-D Arrays

    Let’s reversethe two-dimensional NumPy array using numpy.flip() function. This function takes a 2-D array as input and returns the array in the same shape but reverses the order of elements for each dimension.

    
    # Create NumPy 2-D array
    arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
    
    # Use numpy flip() function with 2-d arrays
    arr2 = np.flip(arr)
    print("Flipped 2D array:\n", arr2)
    
    # Output:
    # Flipped 2D array:
    #  [[9 8 7]
    #  [6 5 4]
    #  [3 2 1]]
    

    <a id="h-5-use-flip-with-axis-1"></a>

    5. Use flip() with axis=1

    If you use axis=1 with numpy.flip() on a 2-D array, it will reverse the order of elements along the columns. The numpy.flip() function is used with axis=1 to reverse the order along columns. The original and flipped arrays are then printed. You will get the reverse array along with column-wise.

    
    # Use numpy.flip() function
    # To reverse the order along columns
    arr2 = np.flip(arr, axis=1)
    print("Flipped 2-D array along columns:\n", arr2)
    
    # Output:
    # Flipped 2-D array along columns:
    #  [[3 2 1]
    #  [6 5 4]
    #  [9 8 7]]
    

    <a id="h-6-use-flip-with-axis-0"></a>

    6. Use flip() with axis=0

    If you use axis=0 with numpy.flip() on a 2-D array, it will reverse the order of elements along the rows. The numpy.flip() function is used with axis=0 to reverse the order along rows. The original and flipped arrays are then printed. You will get the reverse array along with row-wise.

    
    # Use numpy.flip() function
    # To reverse the order along rows 
    arr2 = np.flip(arr, axis=0)
    print("Flipped 2-D array along rows::\n", arr2)
    
    # Output:
    # Flipped 2-D array along rows::
    #  [[7 8 9]
    #  [4 5 6]
    #  [1 2 3]]
    

    <a id="h-7-use-slicing-with-flip"></a>

    7. Use slicing with flip()

    Using the slicing method with flip() function, You can get the specified array in row-wise. For instance, [8 5 2].

    
    # Use slicing with flip() 
    arr2 = np.flip(arr[:,1])
    print("Flipped and sliced 2-D array:\n",arr2)
    
    # Output:
    # Flipped and sliced 2-D array:
    #  [8 5 2]
    

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

    The numpy.flip() function in NumPy is used to reverse the order of elements along a specified axis or axes in a NumPy array. It essentially flips the elements along the chosen axis without changing the shape of the array. If the axis parameter is not specified, the entire array is flattened before flipping.

    You can use numpy.flip() on both 1-D and multi-dimensional arrays. For a 1-D array, the entire array is reversed.

    The shape of the array remains the same after applying numpy.flip(). Only the order of elements along the specified axis or axes is changed.

    You can specify a tuple of integers for the axis parameter to flip along multiple axes. For example, axis=(0, 1) will reverse the order along both rows and columns.

    You can use slicing along with numpy.flip() to achieve more specific flipping effects. For example, you can reverse the order along a specific axis and then extract a portion of the resulting array using slicing.

    numpy.flip() returns a new array with the flipped elements. If you want to modify the original array in place, you can use the numpy.flip method on the array itself, like arr.flip().

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained NumPy flip() function using this how to return a reversed array with shape preserved with examples.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References