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

` ` |

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.