Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
Changing the dimension of a NumPy array is a crucial aspect when you're working with different datasets or trying to manipulate data to fit the requirements of specific algorithms. In this tutorial, you'll learn how to reshape and change the dimensions of NumPy arrays.
Begin by importing the necessary library:
import numpy as np
For demonstration purposes, let's create a one-dimensional array of length 12:
arr = np.arange(12) print(arr)
This will give you:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
a) Into a 2D Matrix
The most common operation is reshaping a 1D array into a 2D matrix. Using the reshape
method, you can convert the array into a matrix of 3 rows and 4 columns:
matrix = arr.reshape(3, 4) print(matrix)
Output:
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
b) Into a 3D Tensor
You can also reshape the array into a 3-dimensional tensor:
tensor = arr.reshape(2, 2, 3) print(tensor)
Output:
[[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]]
c) Using -1 for Automatic Calculation
If you're unsure about the size of a specific dimension, you can use -1
for NumPy to calculate it automatically:
# Reshape to have 4 rows, and let NumPy decide the number of columns matrix = arr.reshape(4, -1) print(matrix)
Output:
[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]
To convert a multi-dimensional array back into a one-dimensional array, you can use ravel()
or flatten()
:
flattened = matrix.ravel() print(flattened)
Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
To swap rows with columns (in the case of 2D arrays), use the T
attribute:
transposed = matrix.T print(transposed)
Output:
[[ 0 3 6 9] [ 1 4 7 10] [ 2 5 8 11]]
Always ensure the new shape is compatible with the number of elements in the array. For example, an array with 12 elements cannot be reshaped into a shape (4, 5)
as that would require 20 elements. An incompatible shape will result in a ValueError
.
Being able to efficiently reshape and manipulate the dimensions of NumPy arrays is a foundational skill in data processing tasks, especially in machine learning and scientific computing scenarios. Remember to keep track of the shape and size of your arrays, and make sure reshapes are feasible given the number of elements.
Reshaping a NumPy array using numpy.reshape()
.
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3, 4, 5, 6]) # Reshaping to a 2D array reshaped_array_2d = np.reshape(array_1d, (2, 3)) print("Original 1D array:", array_1d) print("Reshaped 2D array:\n", reshaped_array_2d)
Changing the shape of a NumPy array using numpy.shape
.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Changing the shape changed_shape = np.shape(array_2d) print("Original 2D array:\n", array_2d) print("Changed shape:", changed_shape)
Resizing a NumPy array using numpy.resize()
.
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3, 4, 5, 6]) # Resizing the array resized_array = np.resize(array_1d, (2, 3)) print("Original 1D array:", array_1d) print("Resized array:\n", resized_array)
Flattening and reshaping a NumPy array using numpy.flatten()
and numpy.reshape()
.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Flattening the array flattened_array = array_2d.flatten() # Reshaping the flattened array reshaped_array = np.reshape(flattened_array, (2, 3)) print("Original 2D array:\n", array_2d) print("Flattened array:", flattened_array) print("Reshaped array:\n", reshaped_array)
Reshaping multi-dimensional arrays in NumPy using numpy.reshape()
.
import numpy as np # Creating a 3D array array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Reshaping to a 2D array reshaped_array_2d = np.reshape(array_3d, (2, 4)) print("Original 3D array:\n", array_3d) print("Reshaped 2D array:\n", reshaped_array_2d)
Manipulating the shape of a NumPy array using numpy.reshape()
.
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3, 4, 5, 6]) # Reshaping to a 2D array reshaped_array_2d = np.reshape(array_1d, (2, -1)) # Use -1 for automatic dimension calculation print("Original 1D array:", array_1d) print("Reshaped 2D array:\n", reshaped_array_2d)
Changing the dimensions of a NumPy array using numpy.reshape()
.
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3, 4, 5, 6]) # Changing dimensions to a 3D array changed_dimensions = np.reshape(array_1d, (1, 2, 3)) print("Original 1D array:", array_1d) print("Changed dimensions (3D array):\n", changed_dimensions)
Transforming array dimensions using numpy.transpose()
.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Transposing array dimensions transposed_array = np.transpose(array_2d) print("Original 2D array:\n", array_2d) print("Transposed array:\n", transposed_array)
Resizing and reordering dimensions in NumPy using numpy.reshape()
.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Resizing and reordering dimensions reshaped_array = np.reshape(array_2d, (1, 3, 2)) print("Original 2D array:\n", array_2d) print("Reshaped array with reordered dimensions:\n", reshaped_array)
Using numpy.transpose()
and numpy.reshape()
for array transpose and reshape.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Transposing and reshaping transposed_reshaped_array = np.transpose(array_2d).reshape((3, 2)) print("Original 2D array:\n", array_2d) print("Transposed and Reshaped array:\n", transposed_reshaped_array)