Updated March 31, 2023
Introduction to numpy.ravel()
numpy.ravel() is a function present in the Numpy toolset which enables the array entered by the user to be contiguously flattening the array. The ravel function in the numpy tool is one of the most essential and commonly used functionalities which helps in unravelling the data which has been presented by the user. In simple words, the ravel function is used to flatten or present the data given in a linear manner, which was previously presented in a dimensional form. Thereby, we can see that it can simplify the structural presentation of an array.
Syntax and Parameters of numpy.ravel()
Following is the syntax in which the numpy.ravel() is written in the Python programming language:
Syntax:
numpy.ravel(a, order='C'/ 'F' / 'A' / 'K')
In accordance with the current version (i.e., Python 1.10) the output returned is of the same type as the array which had been entered by the user. For instance, for an input wear a masked array had been entered by the user, the array which is returned it’s also masked.
Parameters:
The following are the parameters used for the numpy.ravel() function written in the Python programming language:
- a1: array_like
- The array has been entered by the user. The array a1 (here), where the elements have been specified by the user are read by the programming system in the same order as it has been specified and that in the form of a one-dimensional array Order used: { * ‘* C *’ *, *’ * F *’ *, * * ‘* A *’ *, * ‘* K *’ * }, the parameter is optional.
- All the elements that are present in the array a1, when need to be read by the system while using the parameters ‘C’. In such a case, the index of the elements is read on the row-major bases. This means that the index of the last axis change is the fastest while the first axis changes slowest in speed.
- When using the parameter F, the system identifies the respective index number of each of the elements present in the array in the Fortran style. This means that the system identifies the first index to be changing at the fastest pace while the last one would be the slowest in terms of index changing.
- It must be noted that both the parameters F and C do not take any account for the memory layout with respect to the array which has been entered by the user. They only refer to the order in which the axis of the respective array has been indexed.
- While using the parameter A, the system reads the elements of the array a1 in the index order of Fortran only if the array has a Fortran like contagious memory. Otherwise, when parameter A is used the elements are read in a similar way like parameter C.
- While using the parameter K, the elements present in the array given by the user I read in accordance with their occurrence in the memory of the system. The exception being reversal being done in the order in case strides occurring in the data are negative.
- The default parameter when no parameter is specified is C.
Return:
- y1 : array_like
- y1 is the array with a similar subtype as that of the array a1, with the shape being the same, we have to note that matrices are specially cased with respect to the compatibility when spiralling backwards. Note that matrices are special cased for backward compatibility, Example if a1 represents the matrix, then y1 is a one-Dimensional ndarray.
Examples of Using numpy.ravel() in Python
Given below are the examples mentioned:
Example #1
Code:
# Python Program illustrating the implementation of numpy.ravel() function
import numpy as n1
a1 = n1.arange(15).reshape(3, 5)
print("The array entered by the user is :", a1)
# Code flattens the array which was entered creating a one dimensional sequence
# Output for the array is displayed
print("Unravelling the array entered by the user : ", a1.ravel())
# The output array is rechecked for configuring if the order and shape is persistant
print("Reshaping the unravelled array == numpy.reshape(-1)")
print("The reshaped array is : ", a1.reshape(-1))
Output:
Example #2
Code:
# Python Program illustrating the implementation of numpy.ravel() function
import numpy as n1
a1 = n1.arange(15).reshape(3, 5)
print("The array entered by the user is :\n", a1)
# Code flattens the array which was entered creating a one dimensional sequence
# Output for the array is displayed
print("Unravelling the array entered by the user :\n", a1.ravel())
# Using the Parameters for maintaining both 'A' and 'F' order
print("Output using the parameter A for specified order:\n", a1.ravel(order = 'A'))
# Using the Parameters ‘K’ for maintaining both neither of 'A' order nor 'F' order
a2 = n1.arange(12).reshape(2,3,2).swapaxes(1,2)
print("The newly order array which has been remodelled is:\n", a2)
print("Maintaining A order for newly constructed array:\n ", a2.ravel(order = 'K'))
Output:
Conclusion
The function ravel() serves as a very vital tool for programmers dealing with big data problems who have to deal with arrays and data sets with multitudes of dimensions and executing them needs to linearize them in order to separate the elements. This reduces the verbosity and efficiency of the code by presenting an inbuilt function.
Recommended Articles
This is a guide to numpy.ravel(). Here we discuss the introduction to numpy.ravel() along with examples respectively. You may also have a look at the following articles to learn more –