NumPy is a popular Python library that is widely used for working with arrays and performing mathematical operations on them. A key feature of NumPy is its ability to create and manipulate arrays with ease and efficiency. One of the essential functions in NumPy is np.newaxis
, which provides a straightforward way to reshape arrays. In this article, we'll explore how np.newaxis
works and provide some code examples to help you understand its usage.
Understanding np.newaxis
Before we dive into the code examples, it's essential to understand what np.newaxis
does. It is primarily used to insert a new axis into an existing array. In simpler terms, it allows us to change the dimensionality of an array. It is a useful tool when working with arrays of different shapes and sizes to align them in a way that they can be mathematically operated on.
The np.newaxis
object can be used in two different ways:

Explicitly add a new axis to an array.

Implicitly add a new axis to an array by using it as an index.
Adding a new axis explicitly
Let's start with explicit addition. Let's say we have a onedimensional NumPy array, and we want to convert it into a twodimensional array by adding an axis. Here's how we can do it with np.newaxis
:
import numpy as np
# create a onedimensional array
x = np.array([1, 2, 3])
# add a new axis to make it a twodimensional array
y = x[:, np.newaxis]
print(y.shape)
# Output: (3, 1)
In the above example, we're using np.newaxis
to add an extra axis to the onedimensional array x
. When we use x[:, np.newaxis]
, we're telling NumPy to add a new axis at the second position. This results in the creation of a twodimensional array y
of shape (3, 1)
.
We could also add the new axis at different positions depending on the shape we want to achieve. Here's an example:
import numpy as np
# create a onedimensional array
x = np.array([1, 2, 3])
# add a new axis at the beginning to make it a twodimensional array
y = x[np.newaxis, :]
print(y.shape)
# Output: (1, 3)
In the above example, we're adding a new axis at the first position by using x[np.newaxis, :]
. This results in the creation of a twodimensional array y
of shape (1, 3)
.
Adding a new axis implicitly
Another way to use np.newaxis
is to implicitly add a new axis while indexing an array. Here's an example:
import numpy as np
# create a twodimensional array
x = np.array([[1, 2, 3], [4, 5, 6]])
# index using np.newaxis to add a new axis
y = x[:, :, np.newaxis]
print(y.shape)
# Output: (2, 3, 1)
In the above example, we're using np.newaxis
to add a new axis at the last position while indexing the twodimensional array x
. This results in the creation of a threedimensional array y
of shape (2, 3, 1)
.
np.newaxis
can also be used to add a new axis at the beginning and the middle positions. Here's an example:
import numpy as np
# create a twodimensional array
x = np.array([[1, 2, 3], [4, 5, 6]])
# add a new axis at the beginning and the middle position
y = x[:, np.newaxis, :, np.newaxis]
print(y.shape)
# Output: (2, 1, 3, 1)
In the above example, we're using np.newaxis
twice to add a new axis at the beginning and the middle positions while indexing the twodimensional array x
. This results in a fourdimensional array y
of shape (2, 1, 3, 1)
.
Uses of np.newaxis
Now that we have seen how to use np.newaxis
, let's look at why it can be useful. The primary use of np.newaxis
is to convert onedimensional arrays to twodimensional arrays, making them easier to work with. There are other advantages of using np.newaxis
, which we'll explore below.
Broadcasting
Broadcasting is a crucial feature of NumPy arrays. When performing operations between two arrays of different shapes, NumPy will often adjust the shapes automatically to make the operation possible. The way broadcasting works in NumPy is by adding new axes to the smaller array so that its shape matches the larger array. Here's an example:
import numpy as np
# create two arrays of different shapes
a = np.array([1, 2, 3])
b = np.array([[4], [5]])
# add new axis to array a
c = a[:, np.newaxis]
# add arrays
d = c + b
print(d)
# Output: array([[5, 6, 7],
# [6, 7, 8]])
In the above example, we're using np.newaxis
to add a new axis to the onedimensional array a
, making it a twodimensional array. This makes it possible to add it to the twodimensional array b
. The result is another twodimensional array d
, which has the shape (2, 3)
.
Matrix multiplication
Another use case of np.newaxis
is in matrix multiplication. Matrix multiplication can only be performed between arrays that have compatible shapes. By using np.newaxis
, we can change the shape of an array to make it compatible with another array for multiplication. Here's an example:
import numpy as np
# create two arrays of different shapes
a = np.array([1, 2, 3])
b = np.array([[4], [5], [6]])
# add new axis to array a
c = a[:, np.newaxis]
# perform matrix multiplication
d = np.dot(c, b)
print(d)
# Output: array([[32]])
In the above example, we're using np.newaxis
to add a new axis to the onedimensional array a
, making it a twodimensional array. We're then performing matrix multiplication between the twodimensional arrays c
and b
. The result is a onedimensional array d
of shape (1,)
.
Conclusion
In conclusion, np.newaxis
is a powerful tool for reshaping arrays in NumPy. By adding a new axis to an array, we can change its dimensionality and align it with other arrays of different shapes. One of the primary uses of np.newaxis
is to convert onedimensional arrays to twodimensional arrays, which makes them easier to work with. Other use cases of np.newaxis
include broadcasting and matrix multiplication. With an understanding of np.newaxis
, you'll be able to achieve more with NumPy arrays and perform more complex operations with ease.
let's dive a little deeper into some of the topics covered earlier.
Adding a new axis to an array with np.newaxis
As we saw earlier, np.newaxis
is used to add a new axis to an array. This can be very useful when working with arrays of different sizes and shapes. Let's take a closer look at how it works.
When an array is created, it has a certain number of dimensions (also called the array's rank). For example, a onedimensional array (also called a vector) has one dimension, a twodimensional array (also called a matrix) has two dimensions, and so on. However, sometimes we need to reshape an array to make it easier to work with.
This is where np.newaxis
comes in. By adding a new axis to an array, we can effectively reshape the array to have an additional dimension. For example, consider the following code:
import numpy as np
# create a onedimensional array
x = np.array([1, 2, 3])
# add a new axis to make it a row vector
y = x[np.newaxis, :]
print(y)
# Output: array([[1, 2, 3]])
In this code, we use np.newaxis
to add a new axis at the first position of the array x
. This effectively gives us a new row dimension, and x
becomes a row vector. The resulting array y
has shape (1, 3)
, which means it's a onerow, threecolumn matrix.
Implicitly adding a new axis with np.newaxis
Another way to use np.newaxis
is to implicitly add an axis while indexing an array. This can be useful when we want to slice an array in a particular way and reshape it at the same time. Here's an example:
import numpy as np
# create a twodimensional array
x = np.array([[1, 2, 3], [4, 5, 6]])
# add a new axis at the second position while slicing
y = x[:, np.newaxis, :]
print(y.shape)
# Output: (2, 1, 3)
In this code, we use np.newaxis
to add a new axis at the second position while slicing the array x
. This gives us a new depth dimension, and x
becomes a threedimensional array. The resulting array y
has shape (2, 1, 3)
, which means it's a tworow, onecolumn, threedepth array.
Broadcasting arrays with np.newaxis
Another common use of np.newaxis
is in broadcasting arrays of different shapes. Broadcasting is a feature of NumPy that allows us to perform operations between arrays of incompatible shapes. For example, we can add a scalar value to a onedimensional array and NumPy will automatically broadcast the scalar to every element in the array.
Similarly, we can use np.newaxis
to add new dimensions to an array and broadcast it against another array. Here's an example:
import numpy as np
# create two arrays of different shapes
x = np.array([[1, 2], [3, 4]])
y = np.array([10, 20])
# add a new axis to y and broadcast it against x
z = x + y[:, np.newaxis]
print(z)
# Output: array([[11, 12],
# [23, 24]])
In this code, we use np.newaxis
to add a new axis to the onedimensional array y
. This gives us a new row dimension, and y
becomes a tworow, onecolumn matrix. We then add y
to x
using broadcasting, and the result is an array z
of shape (2, 2)
.
Matrix multiplication with np.newaxis
Finally, np.newaxis
can be used to perform matrix multiplication between arrays of different shapes. Matrix multiplication requires that the number of columns in the lefthand matrix matches the number of rows in the righthand matrix. If the matrices don't have matching dimensions, we can use np.newaxis
to reshape the arrays and perform matrix multiplication anyway. Here's an example:
import numpy as np
# create two arrays of different shapes
x = np.array([1, 2, 3])
y = np.array([[4], [5], [6]])
# add new axes to x and y and compute their dot product
z = np.dot(x[:, np.newaxis], y)
print(z)
# Output: array([[ 4, 5, 6],
# [ 8, 10, 12],
# [12, 15, 18]])
In this code, we use np.newaxis
to add a new axis to x
. This gives us a new row dimension, and x
becomes a onerow, threecolumn matrix. We then use np.dot()
to compute the dot product of x
and y
. The resulting array z
has shape (3, 3)
, which means it's a threerow, threecolumn matrix.
Conclusion
In summary, np.newaxis
is a useful tool for working with arrays in NumPy. We can use it to add new dimensions to an array, reshape an array, broadcast arrays, and perform matrix multiplication. By using np.newaxis
effectively, we can create more complex and flexible programs with NumPy.
Popular questions

What is
np.newaxis
used for?np.newaxis
is used to add a new axis to an array, which effectively reshapes the array to have an additional dimension. 
How do you add a new axis to a onedimensional array using
np.newaxis
?To add a new axis to a onedimensional array, you can use the following syntax:
x = np.array([1, 2, 3]) y = x[:, np.newaxis]
This adds a new column dimension to the array
x
and creates a new twodimensional arrayy
. 
How can
np.newaxis
be used for broadcasting?np.newaxis
can be used to add new dimensions to an array and broadcast it against another array. Here's an example:import numpy as np x = np.array([[1, 2], [3, 4]]) y = np.array([10, 20]) z = x + y[:, np.newaxis] print(z) # Output: array([[11, 12], [23, 24]])
In this example,
y[:, np.newaxis]
is used to add a new column dimension toy
, which enables NumPy to broadcasty
to every element inx
. 
How can
np.newaxis
be used for matrix multiplication?We can use
np.newaxis
to perform matrix multiplication between arrays of different shapes. Here's an example:import numpy as np x = np.array([1, 2, 3]) y = np.array([[4], [5], [6]]) z = np.dot(x[:, np.newaxis], y) print(z) # Output: array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])
In this example,
x[:, np.newaxis]
is used to add a new row dimension tox
, which enables NumPy to perform matrix multiplication withy
. 
In what situations might you use
np.newaxis
?np.newaxis
can be useful in situations where you need to reshape an array or add new dimensions to an array to align it with other arrays of different sizes and shapes. It can be used for broadcasting, matrix multiplication, and any other situation where you need to change the dimensions of an array.
Tag
Axis.