Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Differences between Flatten and Ravel Numpy Functions
    python

    Differences between Flatten and Ravel Numpy Functions

    April 7, 2023No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Flatten() and Ravel(), Two Different Numpy Functions and Their Differences

    For the purpose of converting a Ndarray into a 1D array, there are two different sorts of methods: flatten() and also.

    Ravel()



    1. import


      numpy as nmp


    2. P = nmp.array( [ (

      1


      ,


      8


      ,


      4


      ,


      5


      ),(


      4


      ,


      3


      ,


      5


      ,


      1


      ) ] )



    3. #OUTPUT:





    4. print


      ( P.flatten() )



    5. # [ 1,8,4,5,4,3,5,1 ]





    6. print


      ( P.ravel() )



    7. # [ 1,8,4,5,4,3,5,1 ]



    The issue that has to be answered is why there are two separate positions that have the exact same responsibilities.

    job?

    Differences between Flatten() and Ravel()

    P.ravel():

    1. Returns only the reference/view of the original array
    2. In the event that we alter the array, we will be able to see that the value of the original array changes too.
    3. Ravel is faster than flatten() because it doesn’t take up any memory.
    4. Ravel is a library-level function at the library level.

    P.flatten():

    1. Return a duplicate of the initial array
    2. When you alter the value of this array, the original array’s value is not changed.
    3. Flatten() is considerably faster that ravel() because it takes up memory.
    4. Flatten is a method used by a ndarray.

    With this code, let’s examine the difference between the flatter() function and the ravel() function. Code:



    1. import


      numpy as nmp




    2. # Here, we will create a numpy array




    3. P = nmp.array([(

      3


      ,


      4


      ,


      5


      ,


      6


      ),(


      5


      ,


      3


      ,


      6


      ,


      7


      )])




    4. # Now, we will print the array a





    5. print


      (


      “Original array:\n ”


      )



    6. print


      (P)




    7. # For checking the dimension of array (dimension = 2 and type is numpy.ndarray )





    8. print


      (


      “Dimension of array: ”


      , (P.ndim))





    9. print


      (


      “\n The output for RAVEL \n”


      )



    10. # Here, we will convert ndarray to 1D array




    11. Q = P.ravel()



    12. # As the ravel() only passes a view of the original array to array ‘Q’





    13. print


      (Q)


    14. Q[

      0


      ]=


      1000





    15. print


      (Q)




    16. # We can note here that value of the original array ‘P’ at also P[0][0] becomes 1000





    17. print


      (P)




    18. # Just for checking the dimension i.e. 1 and type is same numpy.ndarray )





    19. print


      (


      “Dimension of array”


      ,(Q.ndim))




    20. print


      (


      “\n The output for FLATTEN \n”


      )




    21. # Here, we will convert ndarray to 1D array




    22. R = P.flatten()



    23. # Flatten passes copy of original array to ‘R’





    24. print


      (R)


    25. R[

      0


      ] =


      0





    26. print


      (R)




    27. # Here, we can note that by changing the value of R





    28. # there is no affect on value of original array ‘P’





    29. print


      (P)




    30. print


      (


      “Dimension of array ”


      , (R.ndim))

    Output:

    Original array:
     
    [[3 4 5 6]
     [5 3 6 7]]
    Dimension of array:  2
     The output for RAVEL 
    [3 4 5 6 5 3 6 7]
    [1000    4    5    6    5    3    6    7]
    [[1000    4    5    6]
     [   5    3    6    7]]
    Dimension of array 1
     The output for FLATTEN 
    [1000    4    5    6    5    3    6    7]
    [0 4 5 6 5 3 6 7]
    [[1000    4    5    6]
     [   5    3    6    7]]
    Dimension of array  1
    
    Differences between Flatten and Ravel Numpy Functions Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlespaCy models
    Next Article Deque in Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.