Numpy reshape : 6 Examples to create array

Numpy is a popular Python package that allows you to create a numpy array. You can easily do mathematical calculations using the built-in numpy function. The numpy reshape function is one of them. In this tutorial, you will learn how to use the numpy reshape() function.

What does Numpy reshape do?

The Numpy reshape function allows you to change the shape of any numpy array without changing the data.

Numpy reshape syntax

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

Explanation of the syntax

a: The array you want to be reshaped.
newshape: The new shape of the array. It can be an integer or a tuple of integers that specify the new shape.
order: It is an Optional parameter. It Specifies the order in which the elements of the array should be read. It can be ‘C’ for C-style (row-major) or ‘F’ for Fortran-style (column-major). The default is ‘C’.

Examples of Numpy reshape() function

Example 1: numpy reshape array 1d to 2d

You can use the numpy.reshape() array to convert the the 1d array to 2d array,

import numpy as np

# Create a 1D array
array_1d = np.array([10, 20, 30, 40, 50, 60])

# Reshape the 1D array to a 2D array with 2 rows and 3 columns
array_2d = np.reshape(array_1d, (2, 3))

print("Original 1D array:")
print(array_1d)

print("\nReshaped 2D array:")
print(array_2d)

Output

Original 1D array:
[10 20 30 40 50 60]

Reshaped 2D array:
[[10 20 30]
 [40 50 60]]

Example 2: Use reshape along axis

You can also use the reshape() function to reshape the array along spefici axis.

import numpy as np

# Create a 1D array
array_1d = np.array([10, 20, 30, 40, 50, 60])

# Reshape the 1D array to a 2D array along axis 0 (rows)
array_2d_rows = np.reshape(array_1d, (2, -1), order='C')  # -1 means the size along that axis is inferred

# Reshape the 1D array to a 2D array along axis 1 (columns)
array_2d_columns = np.reshape(array_1d, (-1, 2), order='C')

print("Original 1D array:")
print(array_1d)

print("\nReshaped 2D array along axis 0 (rows):")
print(array_2d_rows)

print("\nReshaped 2D array along axis 1 (columns):")
print(array_2d_columns)

Output

Original 1D array:
[10 20 30 40 50 60]

Reshaped 2D array along axis 0 (rows):
[[10 20 30]
 [40 50 60]]

Reshaped 2D array along axis 1 (columns):
[[10 20]
 [30 40]
 [50 60]]

Example 3: Reshape numpy array from 2d to 3d

Suppose you have a 2d numpy array and want to convert it to a 3d array then you can use the reshape() function.

import numpy as np

# Create a 2D array
array_2d = np.array([[10, 20, 30],
                     [40, 50, 60],
                     [70, 80, 90]])

# Reshape the 2D array to a 3D array with shape (3, 3, 1)
array_3d = np.reshape(array_2d, (3, 3, 1))

print("Original 2D array:")
print(array_2d)

print("\nReshaped 3D array:")
print(array_3d)

Output

Original 2D array:
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Reshaped 3D array:
[[[10]
  [20]
  [30]]

 [[40]
  [50]
  [60]]

 [[70]
  [80]
  [90]]]

Example 4: Reshape the list

You can also reshape the list using this function. You will first convert the list to numpy array and then use the rehsape() function on it.

import numpy as np

# Create a Python list
original_list = [10, 20, 30, 40, 50, 60, 70, 80]

# Convert the list to a NumPy array
array_from_list = np.array(original_list)

# Reshape the 1D array to a 2D array with 2 rows and 4 columns
reshaped_array = np.reshape(array_from_list, (2, 4))

print("Original list:")
print(original_list)

print("\nNumPy array from list:")
print(array_from_list)

print("\nReshaped 2D array:")
print(reshaped_array)

Output

Original list:
[10, 20, 30, 40, 50, 60, 70, 80]

NumPy array from list:
[10 20 30 40 50 60 70 80]

Reshaped 2D array:
[[10 20 30 40]
 [50 60 70 80]]

Example 5: Reshape the numpy array to the square matrix.

You can also create a square matrix using the numpy.reshape() function .

import numpy as np

# Create a 1D array with a length that is a perfect square
array_1d = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Determine the size of the square matrix
matrix_size = int(np.sqrt(array_1d.size))

# Reshape the 1D array to a square matrix
square_matrix = np.reshape(array_1d, (matrix_size, matrix_size))

print("Original 1D array:")
print(array_1d)

print("\nReshaped square matrix:")
print(square_matrix)

Output

Original 1D array:
[10 20 30 40 50 60 70 80 90]

Reshaped square matrix:
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Example 6: Reshape and transpose the array

In this example, I will first convert the 1d array to a 2d array and then transpose the converted 2d array.

import numpy as np

# Create a 1D array
array_1d = np.array([10, 20, 30, 40, 50, 60])

# Reshape the 1D array to a 2D array with 2 rows and 3 columns
array_2d = np.reshape(array_1d, (2, 3))

# Transpose the 2D array
array_transposed = np.transpose(array_2d)  # or array_2d.T

print("Original 1D array:")
print(array_1d)

print("\nReshaped 2D array:")
print(array_2d)

print("\nTransposed 2D array:")
print(array_transposed)

Output

Original 1D array:
[10 20 30 40 50 60]

Reshaped 2D array:
[[10 20 30]
 [40 50 60]]

Transposed 2D array:
[[10 40]
 [20 50]
 [30 60]]

Conclusion

The numpy reshape() function is a very useful function to convert any type of array to another. You can convert a 1d array to a 2d array and a 2d array to a 3d array and so on. In this tutorial, you have learned six examples to implement the numpy.reshape() function.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link