Python numpy reverse array

What are the Different Ways to Reverse a NumPy Array?

Sometimes while programming in Python, we need to use Python to invert a NumPy array. In Python, the reverse of a NumPy array indicates changing the order. The first element will become the last element, the second last, the last the first, and so on. There are several methods for reversing a numpy array.

Some of the most frequent methods for reversing a numpy array are as follows:

  • Utilizing the len() with Loops
  • Using the list slicing technique
  • Using the flip() function
  • Using the reverse() function
  • Using the flipud() function
  • Using the fliplr() function

Ready? Let’s work together to invert some NumPy arrays!

Using the len() with Loops

To reverse a list, we may use the len() method also known as the two-pointers approach. A developer coming from another programming language may use the following technique. This is a custom approach, not a built-in one. We’ll begin by pointing to the first entry on our NumPy array. Start with an index variable pointing to zero. We will now proceed to the last entry of our NumPy array, the end index, which we will compute using the len() method. As a result, these functionalities are dependent on the user’s expertise and how they built the custom code.

Читайте также:  Php get https certificate

Consider the following example:

Using the List Slicing Technique

Let’s look at another method for reversing a one-dimensional array using slicing. The slicing functionality is an expansion of the square brackets feature. This slicing allows us to get to the specific bits we need. We know the array slicing syntax is «array name[Start, Stop, Step]». Start and Stop values are optional , with the default values set to 0 for Start and the array length for Stop. If you enter step «-1» , it means to reverse the array by starting at the end and finishing at the beginning.

Consider the following scenario:

We generated an array with the name arr in the preceding code. After that, we used the slicing notation on the array (arr) to get the results in reverse order. As you may expect from the result, slicing does not destroy the original list.

Slicing a NumPy array is slower than in-place approaches (like the reverse() function; It is explained in detail in the next to next section) because it creates a shallow duplicate of all entries and requires adequate memory to finish the procedure.

Using the flip() Function

The syntax of the numpy.flip() function is as follows.

It is made up of a few parameters.

Sr. No. Parameter Name Parameter Description
1 arr input array
2 axis The default, axis=None, flips all of the input array’s axes.

The numpy.flip() method in NumPy lets you flip or reverse the contents of an array along an axis. When calling numpy.flip() , indicate the array to be reversed as well as the axis. If you don’t provide an axis, NumPy will reverse the contents of your input array along all of its axes.

Note :
Because we’re dealing with multidimensional arrays, reversing an array may be done in various ways. In a 2d array, reversing may be done both row and column-wise.

What is the axis in input ?

The axis input is used to decide whether reversing should be done row or column wise. The orientations along the rows and columns are represented by NumPy axes, like coordinate systems. A NumPy array’s beginning axis is axis 0 . This axis 0 , which runs vertically downward through the rows of NumPy multidimensional arrays, enables column-wise operations. Axis 1 is the second axis of multidimensional NumPy arrays. As a consequence, Axis 1 spans the array columns horizontally. It completes tasks row by row.

With this, we feel you have a good knowledge of how axes may be used to reverse a NumPy multidimensional array.

As illustrated in the following example, this approach does not destroy the original numpy array :

Using the reverse() Function

The reverse() function is a built-in Python technique that allows us to immediately reverse a list. The reverse() function always returns the sequence’s reversed iterator. The biggest disadvantage is that it will function on the original list, which means it will be reversed.

The syntax of the built-in reverse() function is as follows.

The reverse method accepts no arguments.

Let’s look at an example of how to use the reverse() method to create a reverse NumPy array.

Using the flipud() Function

The flipud() method will also reverse the Numpy array items. The numpy. flipud() method flips the array (elements in each column) up and down, keeping the shape.

The flipud() method’s syntax is as follows.

This technique may also be used to reverse a NumPy array, as seen below:

In the preceding code, we loaded the NumPy library and then used the method numpy.array() to build a NumPy array. We made a variable and assigned it the method np.flipud() , which took the array we constructed before and displayed the output. The array was shown in reverse order as a result. We also printed the original array to ensure that it either exists or is destroyed by the flipud() function. However, we discovered that flipud() did not affect the original NumPy array.

Using the fliplr() Function

We may quickly reverse an array using the numpy.fliplr() method. The np. fliplr() function flips the array from left to right. The numpy.fliplr() method always takes an array as an argument and returns the same array with a left-right flip. Assuming you have a matrix, a request occurs requiring you to flip the items in each row while keeping the column intact. This function will come in helpful in this circumstance, and it can be accomplished with only one line of code. Moving on, we’ll take a look at its syntax.

Let’s look at an example of how to use the fliplr() method to create a reverse NumPy array.

A simple example of NumPy fliplr is shown above. In the above example, we first loaded the NumPy module. After that, we defined a two-dimensional array. We printed the result of the numpy.fliplr() method, which validates our input and function. We obtain [23,1], which is the inverse of [ 1 , 2 3 ] [1,23] [ 1 , 2 3 ] , and [ 6 , 3 4 ] [6,34] [ 6 , 3 4 ] , which is the inverse of [ 3 4 , 6 ] [34,6] [ 3 4 , 6 ] . The identical row items are flipped here while the array’s form is retained. We also printed the original array to confirm that it was not destroyed by the fliplr() method. We noticed, however, that fliplr() did not affect the original NumPy array.

Conclusion

We discovered various methods for reversing a NumPy array in Python. We mostly concentrate on built-in methods because custom methods are dependent on the user’s knowledge capabilities. If you’re wondering what the » optimal» technique is in Python to reverse a NumPy array, my answer is «it depends».

This blog taught us:

  • For an array to be flipped, use the len() method.
  • To invert an array, use the list slicing method.
  • numpy.flip() can also be used to flip an array.
  • You may flip an array using the reverse() method.
  • The flipud() method may be used to flip an array.
  • To reverse an array, use the fliplr() method.

Источник

Python NumPy Reverse Array

NumPy reverse array is used to return a new array with the contents of the original array in reverse order. There are various ways to create or initialize reverse arrays in NumPy, for example by using slicing , numpy.flipud() , numpy.flip() , numpy.fliplr() , and reverse() functions. In this article, I will explain how to use Python reverse array in different ways with examples.

1. Quick Examples of Reverse Array

If you are in a hurry, below are some quick examples of NumPy reverse array in Python.

 # Below are the quick examples # Example 1: Use reverse a numpy array with the basic slicing method arr = np.array([2, 5, 8, 14, 25]) arr2 = arr[::-1] # Example 2: Use flipud() function to reverse a numpy array arr2 = np.flipud(arr) # Example 3: Reverse a numpy array using numpy.flip() function arr2 = np.flip(arr) # Example 4: Using reverse() function arr= array.array('i',[2, 5, 8, 14, 25]) arr.reverse() # Example 5: Use np.fliplr() function arr= np.array([[2, 5, 8, 14, 19,25], [3, 6, 9, 13, 18, 29]]) arr2 = np.fliplr(arr) 

2. Reverse NumPy Array Using Basic Slicing Method

We can use the basic slicing method to reverse a NumPy array. Use this syntax [::-1] as the index of the array to reverse it, and will return a new NumPy array object which holds items in a reversed order. Let’s create a NumPy array using numpy.array() and reverse it.

 import numpy as np # Create an 1D input array arr = np.array([2, 5, 8, 14, 25]) # Reverse numpy array use slicing method arr2 = arr[::-1] print(arr2) # Output: # [25 14 8 5 2] 

3. Use flipud() Function to Reverse a Array

The numpy.flipud() function is used to reverse the order or flip the array(entries in each column) in up-down direction. This function preserved the shape of the NumPy array.

3.1 Syntax of NumPy flipud()

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

 # Syntax of flipud() numpy.flipud(arr) 

Following are the parameters of the flipud() function.

It returns flipped array in up-down direction.

3.2 flipud() Example to Reverse Array

Let’s take the same array used in the above example and flip it using flipud() function.

 # Use flipud() function to reverse a numpy array arr2 = np.flipud(arr) print(arr2) 

Yields the same output as above.

4. Use flip() Function to Reverse a Array

Python np.flip() function is another method to reverse the order of array elements along the given axis. The shape of the array is preserved, but the elements are reordered.

4.1 Syntax of NumPy flip()

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

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

4.2 Parameters of flip()

Following are the parameters of the flip() function.

  • arr: Input array.
  • axis: Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis.

4.3 flip() Example

Let’s reverse an array by using np.flip() function. It returns reversed array with shape preserved.

 # Reverse a numpy array using numpy.flip() function arr2 = np.flip(arr) print(arr2) 

Yields the same output as above.

5. Use numpy.fliplr() Function

The numpy.fliplr() function is used to reverse the order of elements for a 2-D array. This flip array entry in each row in the left-right direction. columns are preserved but appear in a different order than before.

5.1 Syntax of NumPy fliplr()

Following is the syntax of the fliplr() function.

 # Syntax of fliplr() numpy.fliplr(arr) 

5.2 Parameters of fliplr()

5.3 fliplr() Example to Reverse Array

By using this fliplr() method let’s reverse the two-dimensional numpy array. This function takes 2-D array as input and returns the array in the same shape but by reversing the order of elements for each dimension.

 # Create a 2D input array arr= np.array([[2, 5, 8, 14, 19,25], [3, 6, 9, 13, 18, 29]]) # Use np.fliplr() function arr2 = np.fliplr(arr) print(arr2) # Output: # [[25 19 14 8 5 2] # [29 18 13 9 6 3]] 

6. Use reverse() Function to Reverse the Array Element

We can also use reverse() function, returns an iterable with elements in reverse order.

 import array # Create an input array arr= array.array('i',[2, 5, 8, 14, 25]) # Using reverse() function arr.reverse() print(arr) # Output: # Array('i', [25, 14, 8, 5, 2]) 

7. Conclusion

In this article, I have explained how to use Python NumPy reverse array in different ways with examples. Also learned how to initialize a reverse array by using NumPy slicing , flipud() , flip() , fliplr() , and reverse() function.

References

You may also like reading:

Источник

Оцените статью