Change language

Differences Between Flatten () and Ravel () | Numpy

| |

We have two similar ways to convert ndarray to one-dimensional array: Flatten () and import numpy as np a = np.array ([(1,7,3,4), (3,2,4,1)]) #OUTPUT: print (a.flatten ()) # [1,7 , 3,4,3,2,4,1] print (a.ravel ()) # [1,7,3,4,3,2,4,1]

Here comes the question: why two identical functions perform the same task?

Differences between Flatten () and Ravel ()

a.ravel () :
(i) Return only reference / view of the original array
(ii) If you change the array, you will notice that the value of the original array also changes. 
(iii) Ravel is faster than flatten () since it doesn’t take up any memory. 
(iv) Ravel — this is a library level function.

a.flatten () :
(i) Return a copy of the original array
(ii) If you change any value this array, it will not affect the value of the original array. 
(iii) Flatten () is comparatively slower than ravel () as it takes up memory. 
(iv) Flatten — this is a method of the ndarray object.

Let’s check the difference with this code

# Python code for differentiation
# between Smooth and Ravel in NumPy

import numpy as np

 
# Create an empty array

a = np.array ([( 1 , 2 , 3 , 4 ), ( 3 , 1 , 4 , 2 )])

 
# Let’s print the array

print ( "Original array:"

print (a)

 
# To check the dimension of the array (dimension = 2) (and type numpy.ndarray)

print ( "Dimension of array-" " , (a.ndim))

  

 

print ( "Output for RAVEL"

# Convert nd array to 1D array

b = a.ravel ()

 
# Ravel only passes a representation of the original array to an array & # 39; b & # 39;

print (b)

b [ 0 ] = 1000

print (b)

 
# Note that the value of the original array & # 39; a & # 39; when a [0] [0] becomes 1000

print (a)

 
# Just to check the dimension, i.e. 1 (and the type is the same numpy.ndarray)

print ( "Dimension of array-"" , (b.ndim))

 

print ( " Output for FLATTEN "

  
# Convert nd array to 1D array

c = a.flatten ()

  
# Flatten transfers a copy of the original array in & # 39; c & # 39;

print (c)

c [ 0 ] = 0

print (c)

 
# Note that changing the value of c does not affect the value of the original array & # 39; a & # 39;

print (a)

 

print ( "Dimension of array-" " , (c.ndim))

 OUTPUT: Original array: [[1 2 3 4] [3 1 4 2]] Dimension of array-" 2 Output for RAVEL [1 2 3 4 3 1 4 2] [1000 2 3 4 3 1 4 2] [[1000 2 3 4] [3 1 4 2]] Dimension of array-> 1 Output for FLATTEN [1000 2 3 4 3 1 4 2] [0 2 3 4 3 1 4 2 ] [[1000 2 3 4] [3 1 4 2]] Dimension of array-" 1 

This article is courtesy of SHAURYA UPPAL .

Please , post comments if you find anything wrong, or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method