Making use of numpy.fliplr() function (3 examples)

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

Introduction

Numpy, a fundamental package for numerical computations in Python, provides the fliplr() function to flip arrays in the left-right direction. This operation is particularly useful in data preprocessing, computer vision tasks, and when dealing with matrix transformations.

Manipulating arrays forms a crucial part of data analysis and machine learning pipelines, often requiring the reorientation of data for various computational needs. The numpy.fliplr() function offers a simplistic yet powerful tool for such operations on two-dimensional arrays. This guide delves into the nuances of utilizing the numpy.fliplr() function through incremental examples, ranging from basic usage scenarios to more sophisticated applications.

The Fundamentals

Syntax:

numpy.fliplr(m)

Parameters:

  • m: array_like. Input array, must be at least 2-D.

Returns:

  • out: ndarray. A view of m with the elements reversed along the second axis. Just like with numpy.flipud(), the returned array shares the same memory as the original array, so modifying one will affect the other.

Basic Example: Flipping an Image

Begin by importing numpy and creating a sample two-dimensional array, which can be thought of as a grayscale image.

import numpy as np

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

# Applying fliplr()
flipped_image = np.fliplr(image)

print(flipped_image)

The output array, a horizontally flipped version of the original, appears as follows:

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

This basic example demonstrates the straightforward utility of fliplr() in reversing the order of elements along the second axis of a matrix.

Intermediate Example: Data Augmentation

This example requires install the scikit-image package:

pip install scikit-image

In the realm of machine learning and computer vision, data augmentation is a technique used to increase the diversity of a dataset by applying a variety of transformations. Flipping images horizontally can simulate different viewing angles, thus enhancing the robustness of models during training.

import numpy as np
from skimage import io

# Loading an image
image = io.imread('path/to/image.jpg')

# Convert the image to RGB if necessary
image = image[:, :, :3]

# Horizontally flip the image
decision = np.random.choice([0,1])
if decision == 1:
    image = np.fliplr(image)

# Display the flipped image (Using a placeholder function to simplify)
display_image(image)

In this scenario, the flipping operation is conditionally applied, introducing variability in a dataset. Note the use of a placeholder “display_image” function for illustrative purposes.

Advanced Example: Matrix Operations

Beyond simple image processing tasks, the fliplr() function can play a pivotal role in complex matrix operations. For instance, consider its application in flipping the columns of a matrix representing a system of linear equations, an operation that might be crucial in certain algebraic manipulations or in creating symmetrical designs.

import numpy as np

# Matrix representing a system of linear equations
A = np.array([[2, 1, -1],
             [-3, -1, 2],
             [-2, 1, 2]])

# Vector representing the constants
b = np.array([8, -11, -3])

# Flipping the matrix A
A_flipped = np.fliplr(A)

print("Original Matrix:\n", A)
print("Flipped Matrix:\n", A_flipped)

This manipulation yields the following results, showcasing the flipped matrix and illustrating its potential impact on systems of equations or design symmetries:

Original Matrix:
 [[ 2  1 -1]
 [-3 -1  2]
 [-2  1  2]]
Flipped Matrix:
 [[-1  1  2]
 [ 2 -1 -3]
 [ 2  1 -2]]

Conclusion

Through these examples, it’s evident that numpy’s fliplr() function is not confined to mere image processing applications but spans a wide array of tasks, including data augmentation and complex matrix operations. Its simplicity and versatility make it an indispensable tool in the arsenal of developers and researchers working with numerical data or arrays. Harnessing the power of numpy through functions like fliplr() can greatly enhance the efficiency and scope of data manipulation tasks.