• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:19 mins read
You are currently viewing Python NumPy Array Reshape

Python NumPy array reshape() method is used to change the shape of a NumPy array without modifying its data. Before going to know the usage of reshape() you need to know about shape(), which is the number of elements in each dimension. Reshaping allows you to alter the structure of the array, adding or removing dimensions, and adjusting the number of elements along each dimension.

Advertisements

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

1. Quick Examples of NumPy Array Reshape

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


# Quick examples of numpy array reshape

# Example 1: Use np.reshape() function
arr = np.array([2, 4, 7, 9, 13, 16, 20, 24, 27, 29, 33, 35])
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: Convert multi dimension array to 1D array
arr = np.array([[2, 4, 7], [9, 13, 16], [20, 24, 27]])
arr2 = np.reshape(arr, -1)

# Example 8: Convert a 2D to 1D NumPy array
arr = np.array([[2, 4, 7], [9, 13, 16], [20, 24, 27]])
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 the 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. In the specific example, you provided, np.reshape(arr, (-1, 3)), the -1 in the first dimension indicates that NumPy should automatically calculate the size of that dimension so that the total number of elements remains constant.

In the below example, the original 1D array is reshaped into a 2D array with 3 columns. The -1 first dimension is inferred by NumPy to be 3, which results in a 3×3 matrix. The total number of elements (9 in this case) remains the same.


# Import numpy
import numpy as np

# Create a 1D array
arr = np.array([2, 4, 7, 9, 13, 16, 20, 24, 27, 29, 33, 35])
print("Original array:\n",arr)

# Use np.reshape() function 
# Reshape the array to a 2D array with 3 columns
arr2 = np.reshape(arr, (-1, 3))
print("Reshaped array:\n",arr2)

Yields below output.

NumPy array reshape

This usage of -1 can be convenient when you want to reshape an array, and don’t want to manually calculate the size of one of the dimensions, letting NumPy handle it based on the size of the array and the specified size of the other dimensions.

3.1 Use reshape() to Convert 1-D Array to 2-D Array

You can use the reshape() function to convert a 1D array to a 2D array. For instance, the original 1D array with 12 elements is reshaped into a 2D array with 4 rows and 3 columns. The reshaped array arr2 has dimensions 4×3, and each row contains 3 elements as specified.


# Reshape from 1-D to 2-D array
arr2 = arr.reshape(4, 3)
print("Converted 2D reshaped array:\n",arr2)

# Output:
# Converted 2D reshaped array:
#  [[ 2  4  7]
#  [ 9 13 16]
#  [20 24 27]
#  [29 33 35]]

3.2 Use reshape() to 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.

In the below, the reshape((2, 2, 3)) function reshapes the original 1D array into a 3D array with dimensions 2x2x3. The resulting array arr2 has two 2×3 matrices, forming a 3D structure.


# Convert 1-D to 3-D Using numpy.reshape()
arr2 = np.reshape(arr, (2, 2, 3))
print("Converted 3D reshaped array:\n",arr2)

Yields below output.


# Output:
Converted 3D reshaped array:
 [[[ 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’

If you want to reshape a 1D array to a 2D array along column-wise with Fortran-style ordering, you can use the reshape() function with the order='F' parameter. For example, reshape((3, 4), order='F') reshapes the original 1D array into a 2D array with 3 rows and 4 columns, and the elements are filled column-wise with Fortran-style ordering.


# Convert 1D array to 2D NumPy array along the column
arr2 = np.reshape(arr, (2, 6), order='F')
print("Converted 2D reshaped array (column-wise with Fortran-style ordering):\n", arr2)

Yields below output.


# Output:
Converted 2D reshaped array (column-wise with Fortran-style ordering):
 [[ 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’

To reshape a 1D array to a 2D array along row-wise with C-style (row-major) ordering, you can use the reshape() function with the order='C' parameter. For instance, reshape((4, 3), order='C') reshapes the original 1D array into a 2D array with 4 rows and 3 columns, and the elements are filled row-wise with C-style (row-major) ordering.


# Convert 1D to 2D array along the row wise with order ‘C’
arr2 = np.reshape(arr, (4, 3), order = 'C')
print("Converted 2D reshaped array (row-wise with C-style ordering):\n", arr2)

Yields below output.


# Output:
Converted 2D reshaped array (row-wise with C-style ordering):
 [[ 2  4  7]
 [ 9 13 16]
 [20 24 27]
 [29 33 35]]

6. Get the Copy of the Original Array

To create a 2D array (arr2) by reshaping a 1D array (arr). Then, it makes a modification to the copied 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.

In the below example, arr2 is created by reshaping the original 1D array into a 2D array with dimensions (2, 6). The .copy() method is then used to make a deep copy of this 2D array. After modifying the copied array (arr2), the original array (arr) remains unchanged.


# Convert 2D array to 1D NumPy array as copy
arr2 = np.reshape(arr, (2, 6)).copy()
arr2[0][0] = 25
print("Copied array after modification:\n",arr2)

# Output:
# Copied array after modification:
#  [[25  4  7  9 13 16]
#  [20 24 27 29 33 35]]

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

You 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-dimensional array to a 1-D array.


# Create a 2D array
arr = np.array([[2, 4, 7], [9, 13, 16], [20, 24, 27]])
print("Original 2D array:\n", arr)

# Covert multi dimension array to 1-D array
arr2 = np.reshape(arr, -1)
print("Converted 1D array:\n", arr2)

# Output:
# Converted 1D array:
 [ 2  4  7  9 13 16 20 24 27]

Alternatively, You can use the reshape() function in NumPy to convert a 2D array to a 1D array. For instance, arr is a 2D array, and arr2 is created by reshaping arr using reshape(-1). The -1 in the reshape function is a placeholder that means “whatever is needed,” and it automatically calculates the size of the missing dimension based on the total number of elements in the original 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]

What does the reshape function do in NumPy?

The reshape() function in NumPy is used to change the shape of an array without modifying its data. It allows you to reorganize the dimensions of the array, adding or removing dimensions, and adjusting the number of elements along each dimension.

Can I reshape a 1D array into a 2D array in NumPy?

You can reshape a 1D array into a 2D array using the reshape() function. For example, np.reshape(arr, (rows, columns)) will reshape a 1D array into a 2D array with the specified number of rows and columns.

How can I convert a 2D array to a 1D array in NumPy?

You can use the reshape() function with the -1 placeholder to convert a 2D array to a 1D array. For example, arr.reshape(-1) will flatten a 2D array into a 1D array.

How can I create a copy of a NumPy array when reshaping?

To create a copy of a NumPy array when reshaping, you can use the copy() method or the np.copy() function. This ensures that the original array and the reshaped array are independent of each other.

What is the default order in NumPy reshape?

The default order for the reshape() function in NumPy is ‘C’, which means C-style or row-major ordering. You can specify the order using the order parameter, and ‘F’ can be used for Fortran-style or column-major ordering.

What is the difference between reshape and resize in NumPy?

The reshape() function returns a new array with the specified shape, while the resize() function modifies the shape of the array in place. resize() can add or remove elements to achieve the desired shape, potentially repeating elements or filling with zeros.

Conclusion

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

Happy Learning!!

References