- 1 Introduction
- 2 Numpy Flatten : np.flatten()
- 3 Numpy Ravel : np.ravel()
- 4 Difference between numpy flatten and numpy ravel
- 5 Conclusion
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.
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.
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
a = np.array([[7,5], [2,6]]) a
array([[7, 5], [2, 6]])
array([7, 5, 2, 6])
Example 2 : numpy.flatten() with 3-D Array
b = np.array([[5,6,4], [1,7,3], [2,9,8]]) b
array([[5, 6, 4], [1, 7, 3], [2, 9, 8]])
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()
Clearly flatten function has no utility over 1-D array because it’s main function is to collapse multidimensional array to 1-D array.
c = np.array([1,2,3,4,5]) c
array([1, 2, 3, 4, 5])
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.
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.
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.
x = np.array([[4, 5],[7,6]]) x
array([[4, 5], [7, 6]])
array([4, 5, 7, 6])
Example 2 : numpy.ravel() with 3-D array
y = np.array([[5,8,2],[4,3,7],[6,9,1]]) y
array([[5, 8, 2], [4, 3, 7], [6, 9, 1]])
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.
z = np.array([3,5,7]) z
array([3, 5, 7])
array([3, 5, 7])
Example 4: Using different order parameter values
a = np.array([[2,5],[7,3]]) a
array([[2, 5], [7, 3]])
# Using default value i.e. 'C' of order parameter np.ravel(a)
array([2, 5, 7, 3])
# Using order parameter value as 'F' np.ravel(a, order='F')
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 returns a copy of the original array
- Values of numpy flatten function remains unaffected if original array values are changed
- Numpy Flatten() is slower and takes more memory than Numpy Ravel()
- Ravel function returns a reference of the original array
- Values of ravel function are changed if original array values are changed
- Ravel() is faster and takes lesser memory in comparison to Flatten()
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.
- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal
- Also Read – Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python