# Tutorial – numpy.flatten() and numpy.ravel() in Python

## Introduction

In this article, we will be covering the numpy flatten and numpy ravel operations. Both these functions numpy.flatten() and numpy.ravel() performs flattening operations over numpy arrays. So let’s start this tutorial. These functions would be useful when manipulating data in your machine learning or data science projects.

### Import Numpy Library

Let’s start by importing numpy library.

In :
```import numpy as np
```

The first function which we are going to learn about is flatten.

## Numpy Flatten : np.flatten()

Numpy flatten function facilitates in providing a copy of an array collapsed into one-dimension.

### Syntax

ndarray.flatten(order=’C’)

order : ‘C’,’F’,’A’,’K’ (optional) – ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten array in the order the elements occur in memory. The default is ‘C’.

The flatten function returns a ndarray type array with a copy of the input array, flattened to one dimension.

### Example 1 : numpy.flatten() with 2-D Array

In :
```a = np.array([[7,5], [2,6]])
a
```
Out:
```array([[7, 5],
[2, 6]])```
In :
```a.flatten()
```
Out:
`array([7, 5, 2, 6])`

### Example 2 : numpy.flatten() with 3-D Array

In :
```b = np.array([[5,6,4], [1,7,3], [2,9,8]])

b
```
Out:
```array([[5, 6, 4],
[1, 7, 3],
[2, 9, 8]])```
In :
```b.flatten()
```
Out:
`array([5, 6, 4, 1, 7, 3, 2, 9, 8])`

We can see that in the above two examples the 2-D array and 3-D array are converted to a flattened array with the help of numpy flatten()

Example 3 : numpy.flatten() with 1-D Array

Clearly flatten function has no utility over 1-D array because it’s main function is to collapse multidimensional array to 1-D array.

In :
```c = np.array([1,2,3,4,5])
c
```
Out:
`array([1, 2, 3, 4, 5])`
In :
```c.flatten()
```
Out:
`array([1, 2, 3, 4, 5])`

### Example 4: Changing the order parameter value from ‘C’ to ‘F’

Here we have changed the order parameter value of flatten function.

In :
```a.flatten('F')
```
Out:
`array([7, 2, 5, 6])`

Let us now see the other function – ravel()

## Numpy Ravel : np.ravel()

The numpy ravel function assists in providing a contiguous flattened array. Now let’s look at how the syntax of numpy.ravel() works.

### Syntax

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

a : array-like – This is the input array. The elements in a are read in the order specified by order, and packed as a 1-D array.

order : ‘C’,’F’,’A’,’K’ – The elements of a are read using this index order. ‘C’ means to index the elements in row-major. ‘F’ means to index the elements in column-major. ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.

The final output of np.ravel() is 1-D array, containing the elements of the input, is returned. A copy is made only if needed.

### Example 1 : numpy.ravel() with 2-D Array

Here we are using the default value of the order parameter of numpy ravel function. Ravel function also collapses 2-D array into 1-D array.

In :
```x = np.array([[4, 5],[7,6]])
x
```
Out:
```array([[4, 5],
[7, 6]])```
In :
```np.ravel(x)
```
Out:
`array([4, 5, 7, 6])`

### Example 2 : numpy.ravel() with 3-D array

In :

```y = np.array([[5,8,2],[4,3,7],[6,9,1]])
y
```
Out:
```array([[5, 8, 2],
[4, 3, 7],
[6, 9, 1]])```
In :
```np.ravel(y)
```
Out:
`array([5, 8, 2, 4, 3, 7, 6, 9, 1])`

### Example 3 : numpy.ravel() with 1-D array

We can see that numpy ravel has no effect on 1-D array, which was the case with numpy flatten as well.

In :
```z = np.array([3,5,7])
z
```
Out:
`array([3, 5, 7])`
In :
```np.ravel(z)
```
Out:
`array([3, 5, 7])`

### Example 4: Using different order parameter values

In :
```a = np.array([[2,5],[7,3]])
a
```
Out:
```array([[2, 5],
[7, 3]])```
In :
```# Using default value i.e. 'C' of order parameter
np.ravel(a)
```
Out:
`array([2, 5, 7, 3])`
In :
```# Using order parameter value as 'F'
np.ravel(a, order='F')
```
Out:
`array([2, 7, 5, 3])`

There is a difference in the ordering of the resulting arrays. This is because ‘C’ indexes the values through rows whereas ‘F’ indexes the values through columns

## Difference between numpy flatten and numpy ravel

### Flatten function

1. Flatten function returns a copy of the original array
2. Values of numpy flatten function remains unaffected if original array values are changed
3. Numpy Flatten() is slower and takes more memory than Numpy Ravel()

### Ravel Function

1. Ravel function returns a reference of the original array
2. Values of ravel function are changed if original array values are changed
3. Ravel() is faster and takes lesser memory in comparison to Flatten()

## Conclusion

Summarizing this brief tutorial, we learned about two basic operations i.e. numpy flatten and numpy ravel, these numpy functions can be performed over arrays for flattening multidimensional arrays to 1-D arrays. We also learned that both np.flatten() and np.ravel() have no effect on 1-D arrays.

Reference-  https://numpy.org/doc/