# Python NumPy ravel() Function Example

Python NumPy ravel() is an inbuilt NumPy function that can be imported as import NumPy as np, and we can create multidimensional arrays and derive other mathematical statistics. The ravel() function is used for returning a 1D array containing all the elements of the n-dimensional input array. If you want to flatten the array then use the numpy ravel() function.

**Python NumPy ravel()**

**Python numpy**.**ravel**(array, order = ‘C’) function returns a contiguous flattened array(1D array with all the input-array elements and with the same type as it). A copy is made only if needed.

**Syntax**

numpy.ravel(a, order='C')

**Parameters**

**a:**This parameter depicts the Input array in which the elements are read in the order specified by an order which gets further packed as a 1D array.**order:**The order argument can be either C_contiguous or F_contiguous, where C order operates row-rise on the array, and F order operates the column-wise operations.

**Return Value**

The ravel() function returns the 1D array containing all the elements of input array with shape (a.size ()).

**Examples**

Program to show the working of ravel function.

import numpy as np arr = np.arange(6).reshape(3, 2) print('The original array:') print(arr, "\n") print('After applying ravel function:') print(arr.ravel()) #Maintaining F order print('ravel function in F-style ordering:') print(arr.ravel(order='F')) #K-order preserving the ordering print("\nnumpy.ravel() function in K-style ordering: ", arr.ravel(order='K'))

**Output**

The original array: [[0 1] [2 3] [4 5]] After applying ravel function: [0 1 2 3 4 5] ravel function in F-style ordering: [0 2 4 1 3 5] numpy.ravel() function in K-style ordering: [0 1 2 3 4 5]

**Explanation**

Here, in the above code, 1st function was used to create a 1D array in which no order was given due to which K type of order was used by default.

2nd function was used to create a 1D array in which F-style ordering was used in which elements are inserted in the array column-wise.

3rd function was used to create a 1D array in which F-style ordering was used in which elements are inserted in the array row-wise.

See the following second code.

import numpy as np arr = np.arange(6).reshape(3, 2) print("Array: \n", arr) # calling the numpy.ravel() function print("\nravel() value: ", arr.ravel()) # ravel() is equivalent to reshape(-1, order=order). print("\nnumpy.ravel() == numpy.reshape(-1)") print("Reshaping array : ", arr.reshape(-1))

**Output**

Array: [[0 1] [2 3] [4 5]] ravel() value: [0 1 2 3 4 5] numpy.ravel() == numpy.reshape(-1) Reshaping array : [0 1 2 3 4 5]

**Explanation**

Here, in the above code,1st function was used to create a 1D array in which no order was given due to which K type of order was used by default.

2nd function was used to create a 1D array using reshape function in which (-1) was passed as an argument which behaves equally as of K type ordering in ravel function.

**Conclusion**

NumPy ravel() function returns the flattened one-dimensional array. The copy is made only if needed. The returned array will have the same type as that of an input array.