# Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python Welcome to yet another tutorial on python numpy arrays. In this post we will understand the concepts of numpy shape, numpy reshape and numpy transpose. All these concepts are related to the dimension of the numpy array and how we can change it. You will need these tricks while manipulating data during your machine learning or data science project.

Before we start, let us first import the numpy library.

### Import Numpy Library

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

## Numpy Shape

Shape is an attribute of numpy array that gives the shape of the array. It returns a tuple containing the dimension of the array. Please do note that numpy shape is a method and not a function.

Let us see some examples below –

### Example 1: One Dimensional Numpy Array

In :
```numpy_array = np.array([5, 12, 8, 21,11])
numpy_array
```
Out:
`array([ 5, 12,  8, 21, 11])`
In :
```numpy_array.shape
```
Out:
`(5,)`

### Example 2: Two Dimensional Numpy Array

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

### Example 3: Three Dimensional Numpy Array

In :
```numpy_array = np.ones((3,2,4))
numpy_array
```
Out:
```array([[[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.]]])```
In :
```numpy_array.shape
```
Out:
`(3, 2, 4)`

## Numpy Reshape

Numpy Reshape takes a numpy array as input and reshapes its dimension with the same data. Unlike the numpy shape that we discussed above, numpy reshape is actually a function and not an attribute.

### Syntax

numpy.reshape(a, newshape, order=’C’)

a – It is the array that needs to be reshaped.

newshape – It denotes the new shape of the array. The input is either int or tuple of int.

order (optional) – Signifies how to read/write the elements of the array. By default, the value is ‘C’. Other options are ‘F’ for Fortran-like index order and ‘A’ for read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Let us now see some examples of numpy reshape to get more clarity.

### Example 1: 1-D array to 2-D array

In :
```numpy_array = np.array([5, 12, 8, 21])
numpy_array
```
Out:
`array([ 5, 12,  8, 21])`
In :
```numpy_array.reshape(2,2)
```
Out:
```array([[ 5, 12],
[ 8, 21]])```

### Example 2: 1-D array to 3-D array

In :
```numpy_array = np.arange(12)
numpy_array
```
Out:
`array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])`
In :
```numpy_array.reshape(3,2,2)
```
Out:
```array([[[ 0,  1],
[ 2,  3]],

[[ 4,  5],
[ 6,  7]],

[[ 8,  9],
[10, 11]]])```

### Example 3: 2-D array to 1-D array

In :
```numpy_array = np.zeros((4,3))
numpy_array
```
Out:
```array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])```
In :
```numpy_array.reshape(12)
```
Out:
`array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])`

### Example 4: Error Scenario

Do notice that when trying to reshape, the new shape should be able to contain all the data of the original array, else it will throw an error.

In :
```numpy_array = np.arange(6)
numpy_array
```
Out:
`array([0, 1, 2, 3, 4, 5])`
In :
```numpy_array.reshape(2,2)
```
Out:
```---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-51-8254effee560> in <module>
----> 1 numpy_array.reshape(2,2)

ValueError: cannot reshape array of size 6 into shape (2,2)```

## Numpy Transpose

Numpy Transpose takes a numpy array as input and transposes the numpy array.

### Syntax

numpy.transpose(a, axes=None)

a – It is the array that needs to be transposed.

axes (optional) – It denotes how the axes should be transposed as per the given value. It is the list of numbers denoting the new permutation of axes.

Let us now see some examples of numpy transpose.

### Example 1:

You can see below that the numpy array’s rows and columns gets transposed.

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

### Example 2:

Numpy transpose has no effect on the 1-D numpy array. It is evident from the below example.

In :
```numpy_array = np.array([5, 12, 8, 21])
numpy_array
```
Out:
`array([ 5, 12,  8, 21])`
In :
```np.transpose(numpy_array)
```
Out:
`array([ 5, 12,  8, 21])`

### Example 3:

In the below example, we are explicitly passing the axes values for transposing.

In :
```numpy_array = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
numpy_array
```
Out:
```array([[1, 2, 3],
[4, 2, 5]])```
In :
```np.transpose(numpy_array, axes=(1,0))
```
Out:
```array([[1, 4],
[2, 2],
[3, 5]])```

## Summary

• Numpy Shape is an attribute that is used to determine the dimension of the numpy array.
• Numpy Reshape is a function that is used to change the dimension of the numpy array, keeping the same data.
• Numpy Transpose is a function that is used to transpose the numpy array.

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