Python NumPy Array Reshape

Python NumPy array reshape() method is used to change the shape of the NumPy array without changing its data. Before going to know the usage of reshape() we need to know about shape(), which is the number of elements in each dimension. Reshaping allows us to add or remove dimensions in an array. You can also change the number of elements in each dimension.

In this article, I will explain how to reshape the Python NumPy array using the numpy.reshape() function with examples.

1. Quick Examples of Python NumPy Array Reshape

If you are in a hurry, below are some quick examples of how to Python NumPy array reshape.


# Below are a quick examples

# Example 1: Use np.reshape() function
arr2 = np.reshape(arr, (-1, 2))

# Example 2: Convert reshape from 1-D to 2-D
arr2 = arr.reshape(4, 3)

# Example 3: Convert a 1D to 3D Using numpy.reshape()
arr2 = np.reshape(arr, (2, 2, 3))

# Example 4: Convert 1D array to 2D NumPy array along the column
arr2 = np.reshape(arr, (2, 6), order='F')

# Example 5: Convert 1D to 2D array along the row wise with order ‘C’
arr2 = np.reshape(arr, (4, 3), order = 'C')

# Example 6: Convert 2D array to 1D NumPy array as copy
arr2 = np.reshape(arr, (2, 6)).copy()
arr2[0][0] = 25

# Example 7: Covert multi dimension array to 1D array
arr2 = np.reshape(arr, -1)

# Example 8: Convert a 2D to 1D NumPy array
arr2 = arr.reshape(-1)

2. Syntax of Array reshape()

Following is the syntax of the NumPy array reshape() function.


# Syntax of reshape()
numpy.reshape(array, newshape, order='C')

2.1 Parameter of reshape()

This function allows three parameters those are,

  • array – The array to be reshaped, it can be a NumPy array of any shape or a list or list of lists.
  • newshape – The new shape should be compatible with the original shape, it can be either a tuple or an int. For converting the shape of 2D or 3D arrays, need to pass a tuple. For creating an array of shape 1D, an integer needs to be passed.
  • order – {‘C’, ‘F’, ‘A’}, optional to read the elements of a using this index order. ‘C’ means to read / write the elements using C-like index order. ‘F’: Read items from array column wise i.e. using Fortran-like index order. ‘A’: Read items from array-based on memory order of items.

2.2 Return Value of reshape()

It returns an array without changing its data.

3. Usage of NumPy Array reshape() Function

NumPy reshape() function is used to change the dimensions of the array, for example, 1-D to 2-D array, 2-D to 1-D array without changing the data. For instance, np.reshape(arr,(-1,3)) changes the 1-D array to 2-D array.

When -1 is used in the dimension, the value is inferred from the length of the array and remaining dimensions.


import numpy as np
 
# creating a numpy array
arr = np.array([2, 4, 7, 9, 13, 16, 20, 24, 27, 29, 33, 35])

# Use np.reshape() 
arr2 = np.reshape(arr, (-1, 3))
print(arr2)

# OutPut
#[[ 2  4  7  9]
# [13 16 20 24]
# [27 29 33 35]]

3.1 Use reshape() convert 1-D Array to 2-D array

Let’s take a 1-d array with 12 elements and convert this 1-dimensional array into a 2-dimensional numpy array or matrix of shape (4 X 3) i.e. 4 rows and 3 columns.


#Reshape from 1-D to 2-D array
arr2 = arr.reshape(4, 3)
print(arr2)

# OutPut
# [[ 2  4  7]
# [ 9 13 16]
#  [20 24 27]
#  [29 33 35]]

3.2 Use reshape() convert 1-D to 3-D NumPy array

To convert a 1-dimensional Numpy array to a 3-dimensional Numpy array use numpy.reshape(). You can pass the 1-dimensional array as the first argument and the new shape i.e. a tuple (2,2,3) as the second argument. It returns a 3-dimensional view of the passed array.


# Convert 1-D to 3-D Using numpy.reshape()
arr2 = np.reshape(arr, (2, 2, 3))
print(arr2)

Yields below output.


# Output
[[[ 2  4  7]
  [ 9 13 16]]

 [[20 24 27]
  [29 33 35]]]

4. Reshape 1-D to 2-D Array along Column wise with order ‘F’

Pass the order parameter as ‘F’ in the np.reshape() function to construct the matrix 2-dimensional array column-wise.


# Convert 1D array to 2D NumPy array along the column
arr2 = np.reshape(arr, (2, 6), order='F')
print(arr2)

Yields below output.


[[ 2  7 13 20 27 33]
 [ 4  9 16 24 29 35]]

5. Reshape 1-D to 2-D Array Along Row wise with order ‘C’

If you pass the order parameter as ‘C’, then items from the input array will be read row-wise.


# Convert 1D to 2D array along the row wise with order ‘C’
arr2 = np.reshape(arr, (4, 3), order = 'C')
print(arr2)

Yields below output.


[[ 2  4  7]
 [ 9 13 16]
 [20 24 27]
 [29 33 35]]

6. Get the Copy of the Original Array

Use numpy.reshape()to return a view of the original array. Now suppose you want to create a 2-dimensional copy of the 1-dimensional NumPy array then use the copy() function along with the reshape() function.


# Convert 2D array to 1D NumPy array as copy
arr2 = np.reshape(arr, (2, 6)).copy()
arr2[0][0] = 25
print(arr2)

# OutPut
# [[25  4  7  9 13 16]
# [20 24 27 29 33 35]]

7. Use Reshape to Convert 2-D to 1-D Array

We can also use reshape() function to convert the 2-D array to a 1-D array. You can use np.reshape(arr,-1), Use -1 as the shape, so that it will convert the array of any shape to a flat array. Follow the below example to convert a multi-dimension array to a 1-D array.


# creating a numpy array
arr = np.array([[2, 4, 7],
                [9, 13, 16],
                [20, 24, 27],
                [29, 33, 35]])

# Covert multi dimension array to 1-D array
arr2 = np.reshape(arr, -1)
print(arr2)

# OutPut
# [ 2  4  7  9 13 16 20 24 27 29 33 35]

Alternatively, You can also use reshape(-1) function to convert a multidimensional array into a 1D array.


# Convert a 2D to 1D NumPy array
arr2 = arr.reshape(-1)
print(arr2)

# OutPut
# [ 2  4  7  9 13 16 20 24 27 29 33 35]

9. Conclusion

In this article, I have explained how to reshape the Python NumPy array using the numpy.reshape() function with examples.

Happy Learning!!

You May Also Like

References

NumPy array reshape

Leave a Reply

You are currently viewing Python NumPy Array Reshape