Tutorial – numpy.flatten() and numpy.ravel() in Python

Tutorial - numpy.flatten() and numpy.ravel() in Python
Python - numpy.flatten() and numpy.ravel()

Introduction

In this article, we will be covering the numpy flatten and numpy ravel operations. Both these functions numpy.flatten() and numpy.ravel() performs flattening operations over numpy arrays. So let’s start this tutorial. These functions would be useful when manipulating data in your machine learning or data science projects.

Import Numpy Library

Let’s start by importing numpy library.

In [1]:
import numpy as np

The first function which we are going to learn about is flatten.

Numpy Flatten : np.flatten()

Numpy flatten function facilitates in providing a copy of an array collapsed into one-dimension.

Syntax

ndarray.flatten(order=’C’)

Ad
Deep Learning Specialization on Coursera

order : ‘C’,’F’,’A’,’K’ (optional) – ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten array in the order the elements occur in memory. The default is ‘C’.

The flatten function returns a ndarray type array with a copy of the input array, flattened to one dimension.

Example 1 : numpy.flatten() with 2-D Array

In [2]:
a = np.array([[7,5], [2,6]])
a
Out[2]:
array([[7, 5],
       [2, 6]])
In [3]:
a.flatten()
Out[3]:
array([7, 5, 2, 6])

Example 2 : numpy.flatten() with 3-D Array

In [4]:
b = np.array([[5,6,4], [1,7,3], [2,9,8]])

b
Out[4]:
array([[5, 6, 4],
       [1, 7, 3],
       [2, 9, 8]])
In [5]:
b.flatten()
Out[5]:
array([5, 6, 4, 1, 7, 3, 2, 9, 8])

We can see that in the above two examples the 2-D array and 3-D array are converted to a flattened array with the help of numpy flatten()

Example 3 : numpy.flatten() with 1-D Array

Clearly flatten function has no utility over 1-D array because it’s main function is to collapse multidimensional array to 1-D array.

In [6]:
c = np.array([1,2,3,4,5])
c
Out[6]:
array([1, 2, 3, 4, 5])
In [7]:
c.flatten()
Out[7]:
array([1, 2, 3, 4, 5])

Example 4: Changing the order parameter value from ‘C’ to ‘F’

Here we have changed the order parameter value of flatten function.

In [8]:
a.flatten('F')
Out[8]:
array([7, 2, 5, 6])

Let us now see the other function – ravel()

Numpy Ravel : np.ravel()

The numpy ravel function assists in providing a contiguous flattened array. Now let’s look at how the syntax of numpy.ravel() works.

Syntax

numpy.ravel(a, order=’C’)

a : array-like – This is the input array. The elements in a are read in the order specified by order, and packed as a 1-D array.

order : ‘C’,’F’,’A’,’K’ – The elements of a are read using this index order. ‘C’ means to index the elements in row-major. ‘F’ means to index the elements in column-major. ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.

The final output of np.ravel() is 1-D array, containing the elements of the input, is returned. A copy is made only if needed.

Example 1 : numpy.ravel() with 2-D Array

Here we are using the default value of the order parameter of numpy ravel function. Ravel function also collapses 2-D array into 1-D array.

In [9]:
x = np.array([[4, 5],[7,6]])
x
Out[9]:
array([[4, 5],
       [7, 6]])
In [10]:
np.ravel(x)
Out[10]:
array([4, 5, 7, 6])

Example 2 : numpy.ravel() with 3-D array

In [11]:

y = np.array([[5,8,2],[4,3,7],[6,9,1]])
y
Out[11]:
array([[5, 8, 2],
       [4, 3, 7],
       [6, 9, 1]])
In [12]:
np.ravel(y)
Out[12]:
array([5, 8, 2, 4, 3, 7, 6, 9, 1])

Example 3 : numpy.ravel() with 1-D array

We can see that numpy ravel has no effect on 1-D array, which was the case with numpy flatten as well.

In [13]:
z = np.array([3,5,7])
z
Out[14]:
array([3, 5, 7])
In [15]:
np.ravel(z)
Out[15]:
array([3, 5, 7])

Example 4: Using different order parameter values

In [16]:
a = np.array([[2,5],[7,3]])
a
Out[16]:
array([[2, 5],
       [7, 3]])
In [17]:
# Using default value i.e. 'C' of order parameter
np.ravel(a)
Out[17]:
array([2, 5, 7, 3])
In [18]:
# Using order parameter value as 'F'
np.ravel(a, order='F')
Out[18]:
array([2, 7, 5, 3])

There is a difference in the ordering of the resulting arrays. This is because ‘C’ indexes the values through rows whereas ‘F’ indexes the values through columns

Difference between numpy flatten and numpy ravel

Flatten function

  1. Flatten function returns a copy of the original array
  2. Values of numpy flatten function remains unaffected if original array values are changed
  3. Numpy Flatten() is slower and takes more memory than Numpy Ravel()

Ravel Function

  1. Ravel function returns a reference of the original array
  2. Values of ravel function are changed if original array values are changed
  3. Ravel() is faster and takes lesser memory in comparison to Flatten()

Conclusion

Summarizing this brief tutorial, we learned about two basic operations i.e. numpy flatten and numpy ravel, these numpy functions can be performed over arrays for flattening multidimensional arrays to 1-D arrays. We also learned that both np.flatten() and np.ravel() have no effect on 1-D arrays.

 

Reference-  https://numpy.org/doc/

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet