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
Adding a new axis to a NumPy array allows you to increase the dimensions of your array. This is particularly useful when, for instance, you want to convert a 1D array into a row or column matrix (2D array). Let's go through the process in detail.
Ensure you have NumPy installed:
pip install numpy
Then, import the necessary library:
import numpy as np
np.newaxis
:The np.newaxis
keyword can be used to increase the dimensions of your existing array.
Convert a 1D array to a column matrix (2D array):
arr = np.array([1, 2, 3, 4, 5]) column_matrix = arr[:, np.newaxis] print(column_matrix) # Outputs: # [[1] # [2] # [3] # [4] # [5]]
Convert a 1D array to a row matrix (2D array):
row_matrix = arr[np.newaxis, :] print(row_matrix) # Outputs: # [[1 2 3 4 5]]
np.expand_dims
:The np.expand_dims
function provides another way to add an axis at a specific location.
arr = np.array([1, 2, 3, 4, 5]) # Convert to a column matrix column_matrix = np.expand_dims(arr, axis=1) print(column_matrix) # Outputs: # [[1] # [2] # [3] # [4] # [5]] # Convert to a row matrix row_matrix = np.expand_dims(arr, axis=0) print(row_matrix) # Outputs: # [[1 2 3 4 5]]
For a 2D array, you can insert a new axis to convert it into a 3D array:
arr_2d = np.array([[1, 2], [3, 4], [5, 6]]) print(arr_2d.shape) # Outputs: (3, 2) arr_3d = arr_2d[:, :, np.newaxis] print(arr_3d.shape) # Outputs: (3, 2, 1)
Similarly, you can insert new axes in various positions in higher-dimensional arrays.
Inserting new axes in a NumPy array is a foundational concept, especially when working with libraries that expect inputs in certain shapes. This technique is particularly useful in machine learning and deep learning contexts, where libraries like TensorFlow and PyTorch often expect data in a certain number of dimensions.
Description: Adding a new dimension to a NumPy array is essential for reshaping and broadcasting operations.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Insert a new axis to create a 2D array arr_2d = np.expand_dims(arr_1d, axis=0) print("Original 1D Array:") print(arr_1d) print("Array with New Dimension:") print(arr_2d)
Description: Demonstrating the creation of a new axis in a NumPy array using the newaxis
keyword.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Add a new axis using newaxis arr_2d = arr_1d[:, np.newaxis] print("Original 1D Array:") print(arr_1d) print("Array with New Axis:") print(arr_2d)
Description: Adding a singleton dimension to a NumPy array using np.newaxis
or None
.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Add a singleton dimension using np.newaxis arr_2d = arr_1d[:, np.newaxis] print("Original 1D Array:") print(arr_1d) print("Array with Singleton Dimension:") print(arr_2d)
Description: The expand_dims
function is used to increase the dimensionality of the input array.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Expand dimensions to create a 2D array arr_2d = np.expand_dims(arr_1d, axis=1) print("Original 1D Array:") print(arr_1d) print("Array with Expanded Dimensions:") print(arr_2d)
Description: Inserting a new axis at a specific position in a NumPy array using np.newaxis
or None
.
Code:
import numpy as np # Create a 2D NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Insert a new axis along the second dimension arr_3d = arr_2d[:, :, np.newaxis] print("Original 2D Array:") print(arr_2d) print("Array with New Axis along the Second Dimension:") print(arr_3d)
Description: Adding a new axis to a 1D NumPy array to transform it into a 2D array.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Insert a new axis to create a 2D array arr_2d = np.expand_dims(arr_1d, axis=0) print("Original 1D Array:") print(arr_1d) print("Array with New Axis:") print(arr_2d)
Description: Using the expand_dims
function to increase the dimensionality of a NumPy array.
Code:
import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3]) # Expand dimensions to create a 2D array arr_2d = np.expand_dims(arr_1d, axis=0) print("Original 1D Array:") print(arr_1d) print("Array with Expanded Dimensions:") print(arr_2d)
Description: Adding a new axis to a multidimensional NumPy array to change its shape.
Code:
import numpy as np # Create a 3D NumPy array arr_3d = np.random.rand(2, 3, 4) # Insert a new axis along the second dimension arr_4d = np.expand_dims(arr_3d, axis=1) print("Original 3D Array:") print(arr_3d) print("Array with New Axis along the Second Dimension:") print(arr_4d)
Description: General manipulation of array dimensions in NumPy using functions like reshape
, transpose
, and expand_dims
.
Code:
import numpy as np # Create a 2D NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Reshape the array reshaped_arr = np.reshape(arr_2d, (3, 2)) # Transpose the array transposed_arr = np.transpose(arr_2d) # Add a new axis along the second dimension new_axis_arr = np.expand_dims(arr_2d, axis=1) print("Original 2D Array:") print(arr_2d) print("Reshaped Array:") print(reshaped_arr) print("Transposed Array:") print(transposed_arr) print("Array with New Axis along the Second Dimension:") print(new_axis_arr)