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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically