Understanding numpy.flipud() function (4 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

In data analysis and scientific computing, numpy stands as a cornerstone library in Python, offering a multi-dimensional array object and a collection of routines for fast operations on arrays. Among its plethora of functions, numpy.flipud() is a simple yet powerful tool for manipulating arrays. This function flips an array up-down (i.e., along the vertical axis). In this article, we’ll explore the numpy.flipud() function through four progressively complex examples. By the end, you’ll have a clear understanding of how and when to use this function in your data manipulation tasks.

Syntax & Parameters

Syntax:

numpy.flipud(m)

Parameters:

  • m: array_like. Input array.

Returns:

  • out: ndarray. A view of m with the elements reversed along the first axis. The returned array shares the same memory as the original array, so modifying one will affect the other.

Example #1 – Basic Usage of numpy.flipud()

The most straightforward use case of numpy.flipud() is to flip a 2D array vertically. Let’s start with a simple example:

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Apply flipud()
flipped_arr = np.flipud(arr)

print(flipped_arr)

Output:

[[7 8 9]
 [4 5 6]
 [1 2 3]]

This example clearly illustrates how flipud() reverses the order of the rows of the array.

Example #2 – Flipping 1D Arrays

Though primarily used with 2D arrays, numpy.flipud() can also work with 1D arrays, effectively acting the same as numpy.flip() since there’s no ‘up-down’ concept in one dimension. Here’s an example:

import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])

# Apply flipud()
flipped_arr = np.flipud(arr)

print(flipped_arr)

Output:

[5 4 3 2 1]

This demonstrates that flipud() can be applied to 1D arrays, although it’s essentially the same as using numpy.flip() in this context.

Example #3 – Flipping Multidimensional Arrays

numpy.flipud() becomes more interesting when applied to arrays with more than two dimensions. It still operates by flipping along the first axis (vertical flip), but the results can seem less intuitive. Let’s examine this with a 3D array example:

import numpy as np

# Create a 3D array
arr = np.array([[[1, 2], [3, 4]],
                [[5, 6], [7, 8]],
                [[9, 10], [11, 12]]])

# Apply flipud()
flipped_arr = np.flipud(arr)

print(flipped_arr)

Output:

[[[ 9 10]
  [11 12]]
 
 [[ 5  6]
  [ 7  8]]
 
 [[ 1  2]
  [ 3  4]]]

This example illustrates how flipud() flips the layers of a 3D array along its vertical axis, which, in this context, is the array’s first dimension.

Example #4 – Advanced Uses: Flipping Along Other Axes

Although numpy.flipud() is specifically designed to flip arrays up-down, you might want to flip arrays along different axes. For such cases, numpy offers generalized functions like numpy.flip(), where you can specify the axis. However, to stick within flipud() functionality, let’s look at a clever workaround using a combination of numpy.rot90() and flipud() to flip arrays left-right as an advanced technique.

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Rotate 90 degrees counter-clockwise
rotated_arr = np.rot90(arr)

# Flip up-down
flipped_rotated_arr = np.flipud(rotated_arr)

# Rotate back
flipped_arr = np.rot90(flipped_rotated_arr, -1)

print(flipped_arr)

Output:

[[3 2 1]
 [6 5 4]
 [9 8 7]]

This demonstrates an inventive use of flipud() in conjunction with rot90() to achieve a left-right flip, showcasing the flexibility of numpy’s array manipulation functions.

Conclusion

The numpy.flipud() function is a versatile and straightforward tool for vertically flipping arrays, from simple 2D arrangements to complex multidimensional data structures. Understanding how to effectively leverage flipud() and other numpy functions can significantly enhance your data manipulation skills, making it easier to preprocess and transform data for analysis or visualization tasks.