HOWTO · NumPy

Add a Dimension to a NumPy Array with numpy.newaxis

Use numpy.newaxis in an array index to add a size-one dimension and form a row or column array.

On this page

numpy.newaxis adds a size-one axis to an array when you use it in an indexing expression. Put it before the existing axis to make a row, or after that axis to make a column. For a one-dimensional array with shape (4,), these choices produce (1, 4) and (4, 1), respectively. The original values remain in the same order; indexing changes the array’s shape by inserting an axis.

Add an Axis to a NumPy Array with numpy.newaxis

numpy.newaxis is an alias for Python’s None object in NumPy indexing. It is not a method that you call with parentheses. The numpy.newaxis documentation describes this indexing constant. In an index, array[np.newaxis, :] places a new axis first, while array[:, np.newaxis] places one after the original axis. The colon keeps all values along that original axis.

Run this example with NumPy installed:

import numpy as np

values = np.array([1, 2, 3, 4])
print(values.shape)

row = values[np.newaxis, :]
column = values[:, np.newaxis]
print(row.shape)
print(column.shape)
(4,)
(1, 4)
(4, 1)

The first shape describes one axis with four entries. In the row form, the inserted first axis has length one and the original axis still has length four. In the column form, the original four-entry axis comes first and the inserted axis has length one. This is why the shapes are (1, 4) and (4, 1): shape entries follow the order of the axes, rather than describing a different set of values. A two-dimensional shape does not by itself make the data a special matrix type; NumPy still represents it as an array.

You can write the same two indexes with None: values[None, :] creates the row shape, and values[:, None] creates the column shape. Choose whichever spelling makes the surrounding code clearer. numpy.newaxis can also add an axis to arrays that already have multiple dimensions. Its location in the index determines the location of the new size-one axis in the resulting shape, so compare the index positions with the shape before relying on a later operation.

Adding an axis is useful when another operation expects dimensions to line up, including some broadcasting operations. The added axis has length one, which can be expanded by broadcasting when the other dimensions are compatible; adding it does not automatically make every pair of arrays compatible. For the row-versus-column choice, focus first on the resulting shape and the axis order needed by the next operation. This keeps the indexing step easy to reason about and helps avoid assuming the two forms are interchangeable.

For an array with shape (2, 3), matrix[:, :, None] appends an axis and gives shape (2, 3, 1), while matrix[None, :, :] prepends one and gives (1, 2, 3). In both cases, the original two axes keep their order and lengths. When you use several index positions, write down the original shape and mark where each None or numpy.newaxis appears; that makes the new shape predictable before you pass the array to another function.