Python: numpy.ravel() function Tutorial with examples

Understanding numpy.ravel() function with its examples in Python

In this article we will discuss about numpy.ravel( ) function and using it in different methods to flatten a multidimensional numpy array.

numpy.ravel( ) is a built-in function provided by Python’s numpy module.

Syntax - numpy.ravel(a, order='C')

where,

  1. a : array_like- It is a numpy array or list where elements will be read in given order
  2. order – The order in which numpy array will be read
  • C‘= Read elements in row by row manner i.e. using C-like index order
  • F‘= Read elements in row by row manner i.e. using Fortan-like index order
  • K‘= Read elements from array based on memory order of items.

Flatten a matrix or 2D array to 1D array using numpy.ravel( ) :

While converting a 2-D array to 1-D array, in numpy.ravel( ) default value of order parameter ‘C’ is passed, so elements in 2-D array is read row by row.

# program :

import numpy as sc

# To create a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
                        [44, 5, 66],
                        [77, 8, 99]])

print('2D Numpy Array:')
print(twoD_array)

# To get a flattened view 1D of 2D Numpy array

flatn_array = sc.ravel(twoD_array)
print('Flattened 1D view:')
print(flatn_array)
Output :
2D Numpy Array:
[[11 2 33]
[44 5 66]
[77 8 99]]
Flattened 1D view:
[11 2 33 44 5 66 77 8 99]

numpy.ravel() returns a view :

It is seen that when we modify the view of the object, the changes is observed not only in flattened 1D object but also in the original 2D Numpy array.

#program :

import numpy as sc

# To create a 2D Numpy array

twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])

flatn_array = sc.ravel(twoD_array)

#Modifying 5th element of the array
flatn_array[4]= 100

print('Modified 2D array:')
print(twoD_array)
print('Modified Flattened 1D view:')
print(flatn_array)
Output :
Modified 2D array:
[[ 11  2  33]
[ 44 100  66]
[ 77  8  99]]
Modified Flattened 1D view:
[ 11  2  33  44 100  66  77  8  99]

Accessing original array from the flattened view object  :

Flattened view object has an attribute base, which points to original Numpy array.     

#Program :

import numpy as sc

# To create a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])

flatn_array = sc.ravel(twoD_array)

#Modifying 5th element of the array
flatn_array[4]= 100

print('Modified Flattened 1D view:')
print(flatn_array)
print('2D Numpy array')
# Here ndarray.base point to retrieve the original Numpy array
print(flatn_array.base)
Output :
Modified Flattened 1D view:
[ 11  2  33  44 100  66  77  8  99]
2D Numpy array
[[ 11  2  33]
[ 44 100  66]
[ 77  8  99]]

Using numpy.ravel() along different axis with order parameter :

ndarray.ravel() accepts parameter order i.e. ‘C’ or ‘F’ or ‘A’. The default value is ‘C’.

Get Flatten view of 2D array Row wise :

If there is mo parameter passed in ravel() function, then default value i.e. ‘C’ is taken. So, now the 2-D array will be read row wise.

# Program :

import numpy as sc

# To create a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])


# getting flattened view of 2D array read row by row
flatn_array = sc.ravel(twoD_array, order='')

print('Flattened View row by row:')
print(flatn_array)
Output :
Flattened View row by row:
[11 2 33 44 5 66 77 8 99]

Get Flatten view of 2D array Column wise :

When we will pass ‘F in ravel() function, then the elements will be read column by column.

#program :

import numpy as sc

# creating a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])

# getting flattened view of 2D array read column by column
flatn_array = sc.ravel(twoD_array, order='F')
print('Flattened View column by column:')
print(flatn_array)
Output :
Flattened View column by column:
[11 44 77 2 5 8 33 66 99]

Get Flatten view of 2D array based on memory layout :

We can also get the transpose view of 2-D Numpy array

# Program :

import numpy as sc

# To create a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])


# To get transpose view of 2D array
transp_arr = twoD_array.T
print('Transpose View of the 2D Numpy Array')
print(transp_arr)
Output :
Transpose View of the 2D Numpy Array
[[11 44 77]
[2   5   8]
[33 66 99]]

We can also get a flattened 1-D view from transpose 2-D Numpy array

#Prtogram :

import numpy as sc

# To create a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])


# To get transpose view of 2D array
transp_arr = twoD_array.T

# To get a flattened view of transpose 2D array
flatn_array = sc.ravel(transp_arr, order='C')

print('Flattened View of transpose 2D Numpy array:')
print(flatn_array)
Output :
Flattened View of transpose 2D Numpy array:
[11 44 77 2 5 8 33 66 99]

In the previous scenario, the original memory layout was ignored while current layout of view object was used. In this case we would try to get flattened view of the transposed Numpy array based on memory layout by using argument as ‘A’. So, in place of current layout in view, memory layout of the original array will be used.

# Program :

import numpy as sc

# creating a 2D Numpy array
twoD_array = sc.array([ [11, 2, 33],
[44, 5, 66],
[77, 8, 99]])

# getting transpose view of 2D array
transp_arr = twoD_array.T

# getting a flattened view of transpose 2D array in row-wise manner
flatn_array = sc.ravel(twoD_array, order='A')

print('Flattened View of transposed Numpy array based on memory layout :')
print(flatn_array)
Output :
Flattened View of transposed Numpy array based on memory layout :
[11 2 33 44 5 66 77 8 99]

Flatten a list of lists using numpy.ravel() :

We can also use list of lists to get flattened view instead of taking array.

# Program :

import numpy as sc

list_lists = [[1, 2, 3, 4],

[4, 5, 6, 7],

[7, 8, 9, 10],

[10, 11, 12, 13]]

# Creating a flattened numpy array from list of lists

flatn_array = sc.ravel(list_lists)

print('Flattened 1D Numpy Array:')

print(flatn_array)
Output :
Flattened 1D Numpy Array:
[ 1  2  3  4  4  5  6  7  7  8  9 10 10 11 12 13]

We can even convert flattened 1-D numpy array to a list.

# Program :

import numpy as sc

list_lists = [[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]]


flatn_array = sc.ravel(list_lists)
print('Flattened List from array:')

# Converting array to list
print(list(flatn_array))
Output :
Flattened List from array:
[1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13]