Difference between Flatten and Ravel Functions in Numpy?
What is the difference between the Numpy flatten and ravel functions? The main functional distinction is that flatten is a function of an ndarray object and hence only works with genuine numpy arrays. ravel(), on the other hand, is a library-level function that may be invoked on any object that can be correctly parsed. For example, ravel() may work on a list of ndarrays, although flatten will not clearly.
That's a fairly clear response, right? Let us go into the intricacies of NumPy flatten and ravel. Following that, we'll go through the differences between flatten and ravel in NumPy in detail.
NumPy is all about ndarrays and high-dimensional arrays. Sometimes you simply want to squish those arrays down to 1 Dimension. This is where ravel or flatten might come in handy. Let's go through their syntax one by one.
numpy.ravel() Function
Syntax :
NumPy’s ravel function can only accept two parameters, one of which is "a," which is used to provide the input array to the function. The second argument is ordered, which is the default parameter. A 1-D array is constructed by packing the items of "a" in the order indicated by the order parameter.
ndarray.flatten() Function
Syntax :
It just needs one argument, order, which is the default parameter. Similar to the ravel function order, this specifies the order in which the NumPy array will be built by packing the items of a ndarray.
Note: The parameter order for the ravel and flatten functions both accepts any value from "C," "F," "A," or "K." 'C' indicates that the items should be indexed in row-major, C-style order, with the final axis index changing the fastest and the first axis index changing the slowest. 'F' indicates that the items should be indexed in column-major, Fortran-style order, with the first index changing the quickest and the final index changing the slowest. The 'C' and 'F' options ignore the underlying array's memory layout and solely pertain to the order of axis indexing. 'A' indicates that the items should be read in Fortran-like indexing if "A" is Fortran contiguous in memory; otherwise in C-like order. 'K' indicates that the items should be read in the order they appear in memory, with the exception of reversing the data when the stride is negative. The 'C' index order is used by default.
Do they resemble each other? Not quite. Let's compare and contrast the flatten and ravel functions.
Difference Between Flatten and Ravel Functions
- The ravel method returns merely a reference/view of the original array, whereas flattening returns an array's copy.
Let us try to explain this with an example:
Code:
Output :
As seen in the above code, when we modified the 2nd index of the "a" array (the result of flattening the original array to a single dimension with the ravel() method), the change was also reflected in the original array, while when we tried to edit the "b" array (is the result of flattening the original array to a single dimension with the flatten() method), the component of array "b" was changed but no impact was noticed on the original array.
- Ravel requires less time than flatten() to squish NumPy arrays down to 1 Dimension.
Flatten(), which yields a copy of the array. Thus we can say that Flatten() is slower than ravel() since it must create fresh memory.
Let us attempt to grasp the following using the following code. With the use of the magic command %%timeit (which is used to measure the execution time of the cell), the following code is evaluated.
Code:
Output :
Code:
Output :
Code:
Output :
Let us attempt to understand the following output. To calculate the execution time in the above code, we utilized the %%timeit magic command. Because we used a smaller array in this example, there was a little difference in the time it took to perform both the ravel and flatten functions.
You shouldn't be concerned, but if the size is huge, flattening will take considerably longer to perform. Obviously, flattening requires more memory than ravel. Let's look at a code example to help clarify things.
Code:
Output :
Code:
Output :
Code:
Output:
As you can see from the above code sample, the ravel() method is significantly quicker than the flatten() function for huge datasets. In our situation, for a 100000000 element array, ravel() took just 165 ns to decrease the dimension of the array to one dimension; however, flatten() took 145 ms. Therefore we can fairly state that flatten() method is substantially slower than ravel() function.
- The ravel method may be applied to any object that can be effectively parsed, whereas flatten is only applicable to ndarray objects.
Let us attempt to comprehend this with an example.
Code:
Output:
We can see in the previous code that ravel successfully squished NumPy arrays down to 1 Dimension. However, when we attempted to compress the list down to one dimension, ravel successfully completed the operation, whereas using the flatten function resulted in an error.
Conclusion
We discussed the key distinctions between the ravel and flatten functions in NumPy. If you're wondering what is the "best" way to compress a NumPy array in Python, a more generic response is "it depends."
This article educated us :
- Flatten always produces a one-dimensional copy, whereas ravel attempts to provide a one-dimensional view of the original matrix. Be cautious since altering the returned matrix from ravel may make changes to the original matrix.
- Modifying the content of the array created by flatten has no effect on the original array.
- Because no memory is duplicated, ravel is frequently quicker, but you must be extra cautious when altering the matrix it returns.
- Because flatten is a function of an ndarray object, it can only be used with legitimate numpy arrays. In contrast, ravel() is a library-level function that may be called on any object that can be appropriately parsed.