Python Numpy Ravel() Explained for Beginners

Introduction

Numpy Ravel() function is used to flatten a multi-dimensional Numpy array into a 1-D array. While flattening the array, it only returns a view of the original array and not a new copy. In this article, we will explore the syntax and various use cases of the np.ravel() function, with detailed examples to explain its functionality.

Syntax

The syntax for Ravel flatten() is as below :

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

Parameters:

  • a: This is the input array that we want to flatten.
  • order: (Optional) This parameter specifies the order in which the elements of the flattened array are arranged. It can take any of the following values –
    • ‘C’: The flattened array is generated in a row-wise manner.
    • ‘F’: The flattened array is generated in a column-wise manner.
    • ‘A’: Behaves like ‘C’ if the array is C-contiguous, and ‘F’ if the array is Fortran-contiguous.
    • ‘K’: Keep the order of the elements in the array as close to the original as possible.

Examples of Numpy Ravel Function

Import Library

We begin with importing the Numpy library as shown below.

In [0]:

import numpy as np

Example 1: Basic Usage of Numpy Ravel()

The first example of numpy.ravel() shows its basic usage, where we pass a 2-D Numpy Array and it returns the flattened view of the same. (Remember, it does not return a new array, but just a view of the original array).

In[1]:

# Create a 2x3 array
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# Flatten the array using np.ravel()
flattened_arr = np.ravel(arr)
print(flattened_arr)

Out[1]:

[1 2 3 4 5 6]

 

Example 2: Using Numpy Ravel() on 3D Array

In this example, we are using numpy ravel() function on the 3D array to flatten it.

In [2]:

# Create a 3x3x3 array
arr = np.arange(27).reshape((3, 3, 3))

print("Original Array:")
print(arr)

# Flatten the array using np.ravel()
flattened_arr = np.ravel(arr)

print("Flattened Array:")
print(flattened_arr)

Out[2]:

Original Array:
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Flattened Array:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26]

Example 3: Using ravel() with order=’C’

This example shows the usage of the order parameter of np.ravel() function. Here we used order=’C’ to flatten the array row-wise. The value C is the default, which means it behaves the same as in the above examples where no order was explicitly provided.

In [3]:

# Create a 2x2 array
arr = np.array([[1, 2],
[3, 4]])

# Flatten the array using np.ravel() with 'C' order (Default)
flattened_arr_c = np.ravel(arr, order='C')

print(flattened_arr_c)

Out[3]:

[1 2 3 4]

 

Example 4: Using Numpy Ravel Function with order=’F’

Here we use order=’F’ which flattens the array column-wise, as evident from the output below.

In [4]:

# Create a 2x2 array
arr = np.array([[1, 2],
                [3, 4]])

# Flatten the array using np.ravel() with 'F' order
flattened_arr_fortran = np.ravel(arr, order='F')
print(flattened_arr_fortran)

Out[4]:

[1 3 2 4]

 

Example 5: Using Ravel Function with order=’A’

When order=’A’ is used it preserves the ordering of the original array. If the original array is row-major, then the array is flattened row-wise, and when the array is column-major, the array is flattened column-wise.

i) Row Major Array

In this example, the 2-D Numpy array is row-major (by default), hence the output of the np.ravel() function with order=’A’ is also row-major.

In [5]:

# Create a 2x2 array
arr = np.array([[1, 2],
                [3, 4]])

# Flatten the array using np.ravel() with 'A' order
flattened_arr_a = np.ravel(arr, order='A')
print(flattened_arr_a)

Out[5]:

[1 2 3 4]

 

ii) Column Major Array

In this example, the 2-D Numpy array is row-major (by default), hence the output of the np.ravel() function with order=’A’ is also row-major.

In [6]:

# Create a 2x2 array
arr = np.array([[1, 2],
                [3, 4]], order='F')

# Flatten the array using np.ravel() with 'A' order
flattened_arr_a = np.ravel(arr, order='A')
print(flattened_arr_a)

Out[6]:

[1 3 2 4]

 

Example 6: Using Ravel Function with order=’K’

The order=’K’ is used to flatten the array as per its original alignment in the memory. It might sound a bit confusing but the below example will help to understand it better.

We first create a 2-D array and then swap its axis. When we use order=’K’ it flattens as per the array’s original layout in memory. In contrast, order=’C’ flattens the version of the array with swapped axes. Hence both outputs are different.

In [7]:

# Create a 2x2 array
arr = np.array([[1, 2],
[3, 4]])

arr = arr.swapaxes(0,1)
print('Array after swaping index')
print(arr)

# Flatten the array using np.ravel() with 'K' order
flattened_arr_k = np.ravel(arr, order='K')
print('\nRavel with order = K')
print(flattened_arr_k)


# Flatten the array using np.ravel() with 'C' order (Default)
flattened_arr_c = np.ravel(arr, order='C')
print('\nRavel with order = C')
print(flattened_arr_c)

Out[7]:

Array after swaping index
[[1 3]
 [2 4]]

Ravel with order = K
[1 2 3 4]

Ravel with order = C
[1 3 2 4]

 

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *