NumPy.ravel() in Python with examples

Numpy

In this article, We have explained NumPy.ravel() in Python with examples. where and how to use all orders supported by the NumPy.ravel() function

What is NumPy.ravel() function in Python

Numpy library provides ravel() function that is used to convert an array from any shape to a 1-D array(flatten array). A 1-D array is a linear array. The array.ravel() function converts the 2-D or N-D arrays to the 1-D linear array.

Syntax

numpy.ravel(array, order='C')

Parameters:

  • Array: If you see the syntax then there are two arguments that this function accepts. The first argument is the array we want to flatten.
  • Order: The second argument is used to provide the order in which we want to flatten our array. It means you can control the order as per your program’s need.
    • C’: Read items from array row-wise i.e.which is C-like index order. The default order index is used for ravel() function is ‘C’
    • ‘F’: Read items from array column-wise i.e. which is Fortran-like index order.
    • ‘A’: means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.
    • K’: Read items from an array based on the memory order of items, except for reversing the data when strides are negative.

Return value :

Return a contiguous flattened array which is a 1-D array, containing the elements of the input array. A copy is made only if needed. The returned array elements will be of the same type as the type of input array elements. (for example, a masked array will be returned for a masked array input)

How does reval() function work?


  • The first argument in the ravel() function is the array we have to provide (samplearray) as input, It returns a flattened array.
  • We saw the default behavior of the ravel() function when we didn’t pass any second argument to it.

Important point: The returned array is a view of original array. All the view concepts will apply to this new array.

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
linear_array = np.ravel(samplearray)
print("Output Returned after ravel:",linear_array)

Output:

(3, 3)
9
Output Returned after ravel: [1 2 3 4 5 6 7 8 9]

reval() function order manipulation Examples


Now let us understand the different flavors of ravel() function where we pass the order argument and decide the output order.

1. Using Row wise order C-like index order

Row-wise order simply means the elements of the input array are read row-wise and arranges in a 1-D array as output. Here C stands for C-like index order.

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
linear_array = np.ravel(samplearray,order = 'C')
print("Array ravel row wise:",linear_array)

Output

Array ravel row wise: [1 2 3 4 5 6 7 8 9]

2. Using Column wise order Fortran like index order.

Column wise order simply means the elements of the input array are read column-wise and arranges in a 1-D array as output. Here F stands for Fortran like index order.

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
linear_array = np.ravel(samplearray,order = 'F')
print("Array ravel Column wise:",linear_array)

Output:

Array ravel Column wise: [1 4 7 2 5 8 3 6 9]

3. Using memory order ‘A’

The third-order that the ravel function supports is Order ‘A’. Before we see the application of the ravel() function by using third-order ‘A’,

  • As per NumPy library documentation: ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, otherwise C-like order.
  • To understand this we must understand what is Fortran contiguous in-memory layout of an array.
  • When we perform an operation on an array then there is a possibility the memory layout is different from the original array.

Example: memory layout of samplearray :

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
print(samplearray.flags)

Output:

C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

If we take a transpose view of this array.

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
Transpose_view = samplearray.T
print(Transpose_view)
print(Transpose_view.flags)

If we check now the transpose_view looks like this:

Output:

[[1 4 7]
 [2 5 8]
 [3 6 9]]
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False


Now if we call the ravel() function on this view then we will get the 1-D array arranged column-wise based on the memory layout which is Fortran contiguous (F_CONTIGUOUS)

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
Transpose_view = samplearray.T
print("Array ravel A Order:",np.ravel(Transpose_view,order= 'A'))

Output:

Array ravel A Order: [1 2 3 4 5 6 7 8 9]

Similarly if we call a ravel on samplearray , we will get 1-D array arranged in row wise because it is C_CONTIGUOUS.

print("Array ravel A Order:",np.ravel(samplearray,order= 'A'))

Output:

Array ravel A Order: [1 2 3 4 5 6 7 8 9]

4.using memory order ‘K’

‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative.

import numpy as np
samplearray = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
Transpose_view = samplearray.T
print("Array ravel K Order:",np.ravel(Transpose_view,order= 'K'))
print("Array ravel K Order:",np.ravel(samplearray,order= 'K'))

Output:

Array ravel K Order: [1 2 3 4 5 6 7 8 9]
#Array ravel K Order: [1 2 3 4 5 6 7 8 9]

Another example is:

temp = np.arange(9).reshape(3,3).swapaxes(1,0)
print("temp array",temp)
print("C Order ravel on temp",np.ravel(temp,order='C'))
print("K Order ravel on temp",np.ravel(temp,order='K'))

Output

temp array [[0 3 6]
 [1 4 7]
 [2 5 8]]
C Order ravel on temp [0 3 6 1 4 7 2 5 8]
K Order ravel on temp [0 1 2 3 4 5 6 7 8]