## How to reshape a numpy array?

The `numpy.reshape()`

function is used to reshape a numpy array without changing the data in the array. It is a very common practice to reshape arrays to make them compatible for further calculations.

In this article, you will learn about the possible use cases of the `numpy.reshape`

function.

## numpy.reshape

**Syntax:**numpy.reshape(a, newshape, order=’C’)**Purpose:**Gives a new shape to the array without changing the data**Parameters:****a:**_array*like*Array to be reshaped**newshape:***int or tuples of ints*Should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.**order:***{‘C’, ‘F’, ‘A’}, optional*Read the elements of a using this index order, and place the elements into the reshaped array using this index order. Detailed usage will be discussed further.

**Returns**reshaped_array*ndarray*

```
# Import Packages
import numpy as np
```

## 1. Numpy.reshape function

The `numpy.reshape()`

function is used to change the shape of the numpy array without modifying the array data. To use this function, pass the array and the new shape to `np.reshape()`

. The shape argument should be passed in the form either “tuple” or “int”.

Let’s understand this with examples.

### Reshaping 1-D array into a 2-D array

In this example, you have to transform a 1-dimensional array of shape (8,) to 2-dimensional array of shape (4,2).

**Step 1: Create a numpy array of shape (8,)**

```
num_array = np.array([1,2,3,4,5,6,7,8])
num_array
```

`array([1, 2, 3, 4, 5, 6, 7, 8])`

**Step 2: Use np.reshape() function with new shape as (4,2)**

```
np.reshape(num_array, (4,2))
```

```
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
```

As you can see the shape of the input array has been changed to a (4,2). This is a 2-D array and contains the smae data present in the original input 1-D array

### Reshaping 2-D array into a 3-D array

In this example, you have to transform a2-dimensional array of shape (4,2) to 3-dimensional array of shape (2,2,2).

**Step 1: Create a numpy array of shape (4,2)**

```
num_array = np.array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
num_array
```

```
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
```

**Step 2: Use np.reshape() function with new shape as (2, 2, 2)**

```
np.reshape(num_array, (2,2,2))
```

```
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
```

As you can see the shape of the input array has been changed to a (2, 2, 2). This is a 3-D array and contains the smae data present in the original input 2-D array.

## 2. Can you reshape the numpy array into any shape?

The `np.reshape()`

function returns the transformed array with the new shape provided in the function. The only condition is that the number of elements in the original array and the number of elements in the transformed array should be equal.

If you don’t know how to find out the number of elements in an array, simply multiply the number of elements per axis/dimension. It simply means multiplication of all the numbers mentioned in the shape tuple.

#### Get Free Complete Python Course

Facing the same situation like everyone else?

Build your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable.

#### Get Free Complete Python Course

Build your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable.

Let’s see what happens if you try to reshape an array with unequal elements

**Step 1: Create a numpy array of shape (5,)**

```
a = np.array([1,2,3,4,5])
a
```

```
array([1, 2, 3, 4, 5])
```

**Step 2: Use np.reshape() function with new shape as (2,5)**

```
np.reshape(a, (2,5))
#> Throws ValueError
```

In this case, a `ValueError`

exception is raised. The problem here is that the original array has 5 elements. But the new shape, defined as `(2,5)`

, expects 2×5=10 elements. There is a mismatch of the number of elements and therefore, the code failed.

But what if you wish to reshape to an unknown dimension?

You can use -1 for the unknown dimension.

## 3. What is the meaning of -1 in numpy.reshape?

The `np.reshape()`

function supports powerful usage of unknown dimensions or placeholder `(-1)`

.

While defining a new shape, you can put one of the dimensions as unknown. Numpy will automatically infer the right dimension for that particular shape. This is to ensure that the input and output arrays have the same number of elements.

It can be useful in cases when the exact dimensions of the input array is not known but some of the output dimensions are known. Let’s see an example where the input array dimensions are not known but 2 rows are required in the output array.

**Step 1: Create a numpy array**

```
num_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
num_array
```

```
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
```

**Step 2: Use np.reshape() function with new shape as (2,-1)**

```
np.reshape(num_array, (2,-1))
```

```
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
```

The given input array is a 3-D array with shape (2,2,2).

So what does the -1 in (2, -1) mean?

The -1 informs numpy to automatically infer the dimension of that axis. So, on applying `np.reshape()`

function for shape `(2,-1)`

, Numpy is able to infer the last dimension as 4 automatically.

But what happens if you don’t even put the 1 in the dimension of the output array and simply use just -1 instead?

## 4. Flatten the arrays

This is an extended use case of using unknown dimensions for reshaping numpy arrays. Unknown dimensions placeholder `(-1)`

allows the dimensions to be inferred automatically by numpy. This trick can be used to flatten an array. If `(-1)`

placeholder is placed in the `np.reshape()`

function, then the function returns a flatten array,

Let’s see an example below.

**Step 1: Create a 3-D numpy array**

```
a_3d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
a_3d_array
```

```
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
```

**Step 2: Use np.reshape() function with new shape as (-1)**

```
np.reshape(a_3d_array, (-1))
```

```
array([1, 2, 3, 4, 5, 6, 7, 8])
```

Here, a 3-D array with shape `(2,2,2)`

is flattened to a 1-D array.

## 5. How to change the ordering of numpy reshape process?

On an abstract level, the `np.reshape()`

function works in such a way that it starts with an unrolling or flattening process. This is where all the elements of the input array are flattened into a 1-D array and then rolled back or reshaped according to the input shape provided. But how does numpy decide which dimension element would be flattened first and in which order the elements would be put back? And what if you wish to change this order?

This order of unrolling can be controlled using the `order`

parameter in the `np.reshape()`

function. This parameter can take 3 values:

`C`

: C-like order index`F`

: Fortran-like order index`A`

: Either in C order or in Fortran order

Let’s discuss each of them.

### C-like order index

C here stands for the C-language and this order is known as the C-like order index. According to this order, the last index or dimension of the array changes the fastest, and the first index changes the slowest.

In simple terms, the unrolling starts with the last dimension elements and then proceeds towards the first dimension elements. This ordering is maintained for the rolling back process too when the output array is being created. It is also the default value in `np.reshape()`

function.

Let’s see an example below.

**Step 1: Create a 2-D numpy array**

For this case, let’s create a special array. The elements of this 2-D array will correspond to the respective row and column numbers. For example, the element “R1C2” represents elements in the 1st row and 2nd column.

```
a_2d_array = np.array([['R1C1', 'R1C2'], ['R2C1', 'R2C2'], ['R3C1', 'R3C2']])
a_2d_array
```

```
array([['R1C1', 'R1C2'],
['R2C1', 'R2C2'],
['R3C1', 'R3C2']], dtype='<U4')
```

**Step 2: Use np.reshape() function with new shape as (2,3) and order C**

```
np.reshape(a_2d_array, (2,3), order='C')
```

```
array([['R1C1', 'R1C2', 'R2C1'],
['R2C2', 'R3C1', 'R3C2']], dtype='<U4')
```

The output above clearly indicates that in the last dimension of the input 2-D array, columns were flattened first. The elements were flattened in the order “R1C1”, “R1C2”, “R2C1”, and so on. Then during the reshaping process, “R1C1” was placed in 1st row, 1st col, “R1C2” was placed in 1st row, 2nd column, and “R2C1” was placed in 1st row, 3rd column.

“R2C1” was placed in such a manner so that the output array shape becomes compatible with the input array shape.

### Fortran-like order index

F here stands for the language Fortran. Here, the first index or dimension changes the fastest, and the subsequent index changes the slowest. In other words, unrolling process starts with the first dimension and then it proceeds towards the last dimension. This ordering is maintained for the rolling back process too.

Let’s see an example below.

**Step 1: Create a 2-D numpy array**

```
# Using the same array created in 'C' order
a_2d_array
```

```
array([['R1C1', 'R1C2'],
['R2C1', 'R2C2'],
['R3C1', 'R3C2']], dtype='<U4')
```

**Step 2: Use np.reshape() function with new shape as (2,3) and order F**

```
np.reshape(a_2d_array, (2,3), order='F')
```

```
array([['R1C1', 'R3C1', 'R2C2'],
['R2C1', 'R1C2', 'R3C2']], dtype='<U4')
```

The output above depicts that in the first dimension of the input 2-D array, rows were flattened first. The elements were flattened in the order “R1C1”, “R2C1”, “R3C1”, and so on. Then during the reshaping process, “R1C1” was placed in 1st row, 1st column, “R2C1” was placed in 2nd row, 1st column, and “R3C1” was placed in 1st row, 2nd column.

“R3C1” was placed in such a manner so that the output array shape becomes compatible with the input array shape.

### A order

This type of order does not have any specific rules. It depends on how the array is stored in the memory. If the array is stored in a C-like memory, then the `C`

order is used and if the array is stored as Fortran-like memory, then the `F`

order is used. A user is not aware of what the output result would be and that’s why this ordering is rarely used.

## 6. Alternate ways of reshaping arrays

Although, a numpy array can be reshaped using `np.reshape()`

function but, there are some alternate methods. Two such methods are:

- Numpy array object function
- Using
`np.ravel()`

in combination with`np.reshape()`

Let’s explore these methods.

### Numpy array object function for reshaping arrays

A numpy array object supports almost all the operations that can be performed using the numpy explicit functions. The numpy array can be reshaped by accessing the `.reshape()`

function from the numpy array object. See can example below.

**Step 1: Create a numpy array of shape (8,)**

```
num_array = np.array([1,2,3,4,5,6,7,8])
num_array
```

```
array([1, 2, 3, 4, 5, 6, 7, 8])
```

**Step 2: Use .reshape() function from numpy array object with new shape as (4,2)**

```
num_array.reshape((4,2))
```

```
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
```

### Using np.ravel() in combination with np.reshape() for reshaping arrays

`np.ravel()`

function is used for flattening the numpy arrays. It returns the multidimensional array as a flattened contiguous array. This function can be used in combination with `np.reshape()`

function. The result of the ravel function can be passed into the reshape function with a new shape defined and it will still return the correct results.

Let’s see an example.

**Step 1: Create a numpy array of shape (8,)**

```
num_array = np.array([1,2,3,4,5,6,7,8])
num_array
```

```
array([1, 2, 3, 4, 5, 6, 7, 8])
```

**Step 2: Use np.reshape() and np.ravel() function with new shape as (4,2)**

```
np.reshape(np.ravel(num_array), (4,2))
```

```
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
```

## 7. Advanced reshaping

One of the alternative ways to reshape a numpy array, as mentioned in the above section, is to flat the array using `np.ravel()`

function. Then use the output of ravel function as input for `np.reshape()`

function along with the new shape for the final output array.

`np.ravel()`

also supports `order`

parameter and it works the same way as in the `np.reshape()`

function. Therefore, one can have a different order for flattening and reshaping. Let’s discuss these cases.

### Case 1: Flattening in C order, reshaping in F order

In this case, the array is flattened using the `np.ravel()`

function along with `order`

parameter `C`

. For the `np.reshape()`

function, the order parameter would be `F`

.

**Step 1: Create a numpy array of shape (8,)**

```
num_array = np.array([1,2,3,4,5,6,7,8])
num_array
```

```
array([1, 2, 3, 4, 5, 6, 7, 8])
```

**Step 2: Use np.reshape() and np.ravel() function with new shape as (4,2)**

```
np.reshape(np.ravel(num_array, order='C'), (4,2), order='F')
```

```
array([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
```

### Case 2: Flattening in F order, reshaping in C order

In this case, the array is flattened using the `np.ravel()`

function along with `order`

parameter `F`

. For the `np.reshape()`

function, the order parameter would be `C`

.

**Step 1: Create a numpy array of shape (8,)**

```
num_array = np.array([1,2,3,4,5,6,7,8])
num_array
```

```
array([1, 2, 3, 4, 5, 6, 7, 8])
```

**Step 2: Use np.reshape() and np.ravel() function with new shape as (4,2)**

```
np.reshape(np.ravel(num_array, order='F'), (4,2), order='C')
```

```
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
```

## 8. Test your knowledge

**Q1:** What if a negative index is passed in the shape tuple? (Assume only one negative index)

**Answer:** The numpy will automatically infer the -1 as a missing dimension and choose the correct dimension of its own

**Q2:** What is the difference between the ‘C’ and ‘F’ order?

**Answer:** In ‘C’ order, the last index or dimension of the array changes the fastest, and the first index changes the slowest. But in ‘F’ order, the first index or dimension changes the fastest, and the subsequent index changes the slowest.

**Q3:** List two ways to flatten a numpy array.

**Answer:**

1) Using `np.ravel()`

function

2) Using unknown dimension placeholder `-1`

in `np.shape()`

function

The Article was contributed by Kaustubh G.