w3resource

A Guide to numpy.newaxis for Array Dimension manipulation


Detailed Explanation of numpy.newaxis in Python

numpy.newaxis is used to increase the dimensions of an existing array by one. It provides an alias for None in the context of slicing, enabling the creation of higher-dimensional arrays without modifying the data. This is especially useful for broadcasting and reshaping arrays.


Syntax:

python
Copy code
array[:, np.newaxis]

numpy.newaxis is placed within slicing brackets ([...]) to add a new axis.


Examples:

Example 1: Adding a New Axis to a 1D Array

Code:

import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3])

# Add a new axis to make it a column vector
column_vector = arr[:, np.newaxis]

# Add a new axis to make it a row vector
row_vector = arr[np.newaxis, :]

# Print results
print("Original array shape:", arr.shape)
print("Column vector shape:", column_vector.shape)
print("Row vector shape:", row_vector.shape)

Explanation:

  • The original array has a shape of (3,) (1D).
  • Using np.newaxis, the shape becomes (3, 1) (column vector) or (1, 3) (row vector).

Example 2: Adding Axes to a 2D Array

Code:

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Add a new axis at the beginning
new_axis_first = arr[np.newaxis, :, :]

# Add a new axis at the end
new_axis_last = arr[:, :, np.newaxis]

# Print results
print("Original shape:", arr.shape)
print("Shape after adding new axis at the beginning:", new_axis_first.shape)
print("Shape after adding new axis at the end:", new_axis_last.shape)

Explanation:

  • Adding a new axis at the beginning creates a 3D array with shape (1, 2, 3).
  • Adding a new axis at the end creates a 3D array with shape (2, 3, 1).

Example 3: Broadcasting with numpy.newaxis

Code:

import numpy as np

# Create two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5])

# Add a new axis to 'a' for broadcasting
broadcast_result = a[:, np.newaxis] + b

# Print the result
print("Result of broadcasting:\n", broadcast_result)

Explanation:

  • The shape of a becomes (3, 1) using np.newaxis, making it compatible with b (shape (2,)) for broadcasting.
  • The resulting array has a shape of (3, 2).


Example 4: Reshaping Images with numpy.newaxis

Code:

import numpy as np

# Simulate a grayscale image (2D array)
image = np.array([[1, 2], [3, 4]])

# Add a channel dimension
image_with_channel = image[:, :, np.newaxis]

# Print results
print("Original shape:", image.shape)
print("Shape after adding channel dimension:", image_with_channel.shape)

Explanation:

  • In image processing, np.newaxis is often used to add channel dimensions (e.g., converting 2D grayscale images to 3D arrays).


Applications of numpy.newaxis:

    1. Reshaping Arrays:
    Useful for converting 1D arrays to 2D row or column vectors.

    2. Broadcasting:
    Enables broadcasting by aligning array shapes.

    3. Image Processing:
    Adds channel dimensions for compatibility with models or libraries.

    4. Matrix Operations:
    Simplifies aligning dimensions for operations like dot products or tensor multiplications.


Additional Notes:

    1. Alias for None:
    np.newaxis is equivalent to using None. For example, arr[:, np.newaxis] is the same as arr[:, None].

    2. Higher-Dimensional Arrays:
    It can be used multiple times in slicing to add multiple axes simultaneously.

Practical Guides to NumPy Snippets and Examples.



Follow us on Facebook and Twitter for latest update.