Differences between Flatten() and Ravel() Numpy Functions

Differences between Flatten() and Ravel() Numpy Functions

There are two kinds of methods to convert a Ndarray into a 1D array flatten() as well as Ravel()

The question here is, what is the reason there are two distinct roles to perform the same 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.

Let’s see the difference between flatter() and ravel() function using this code.

Code:

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263151.html

(0)
上一篇 2022年5月30日 17:03
下一篇 2022年5月30日 17:03

相关推荐

发表回复

登录后才能评论