Updated April 14, 2023
Introduction to NumPy Newaxis
Numpy newaxis is one of the various functions supported by python numpy library that allows us to change or expand the dimension of a numpy array in the position in which it has been entered. When we include each of the newaxis objects in the tuple of our variable or array we will get a new additional dimension for our array in the same position where we have included the newaxis object.
Syntax
The basic syntax of the numpy Newaxis function is,
np.array()[numpy.newaxis]
Numpy Newaxis is an object that is included in the array to expand the dimension of the given array. The dimension can be expanded in the position with respect to the position of the newaxis object.
Examples of NumPy Newaxis
Here are the following examples as mentioned below:
Example #1
Let us discuss a basic example for understanding how the numpy newaxis function works.
Code:
import numpy as np
a = np.array(7)
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
b=np.array(a)[np.newaxis]
print('array: \n', b)
print('array.ndim:', b.ndim)
print('array.shape:', b.shape)
Output:
Here in the above example, we have called the numpy library for our np.newaxis function. We have declared an array ‘a’ which is a single dimension array having only one element and the corresponding value, dimension, and shape of the array ‘a’ is printed to confirm that its a zero-dimensional array 0-D. Now to convert 0-D array to one-dimensional array we use the np.newaxis function in our new array ‘b’ and the corresponding value, dimension, and shape of the array ‘a’ is printed it confirms the expansion of the 0-D array to 1-D array.
Example #2
In this example well discuss how to change the one-dimensional array to a two-dimensional array using the newaxis object.
Code:
import numpy as np
a=np.array([3,4,5])
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
b=np.array(a)[np.newaxis]
print('array: \n', b)
print('array.ndim:', b.ndim)
print('array.shape:', b.shape)
Output:
In this example, we have converted a one-dimensional array to a two-dimensional array by using the numpy newaxis function. We have created an array ‘a’ as a one-dimensional array and we have printed its value, dimension, and shape. The array ‘b’ is an extension of array ‘a’ with an expanded dimension using the np.newaxis object inside the tuple. The resulting array as we can see is a two-dimensional array and we have printed its value, dimension, and shape.
Example #3
In this example, we have created a zero-dimensional array and converted it into a two-dimensional array.
Code:
import numpy as np
a=np.array(5)
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
c=np.array(a)[np.newaxis,np.newaxis]
print('array: \n', c)
print('array.ndim:', c.ndim)
print('array.shape:', c.shape)
Output:
The array ‘a’ as we did in example 1 is a zero-dimensional (0-D) array and we have printed its value, dimension, and shape of the array ‘a’. Now using the numpy newaxis object in the array ‘a’ we have directly converted a one-dimensional array ‘a’ into a two-dimensional array ‘c’. We have declared the np.newaxis twice inside the tuple so it represents the expansion of dimension into two times. So the corresponding array ‘c’ which we got is a two-dimensional array.
Example #4
In this example, we’ll see how we can expand a zero-dimensional array into a three-dimensional array.
Code:
import numpy as np
a=np.array(5)
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
c=np.array(a)[np.newaxis,np.newaxis,np.newaxis]
print('array: \n', c)
print('array.ndim:', c.ndim)
print('array.shape:', c.shape)
Output:
The array ‘a’ we have created is similar to previous examples which is a one-dimensional array. The array ‘c’ we have created is an expansion of array ‘a’ into a three-dimensional array and we have done that using the numpy newaxis function thrice inside the tuple along with the array ‘a’ and the resultant array is a three-dimensional array of shape (1,1,1). So using this technique we can create n-number of dimensions in an array.
Example #5
In this example, we’ll try to expand the dimension of a one-dimensional array into a two-dimensional array along with changing the shape of the resulting array using the newaxis object.
Code:
import numpy as np
a=np.array([5,10,15])
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
c=np.array(a)[:,np.newaxis]
print('array: \n', c)
print('array.ndim:', c.ndim)
print('array.shape:', c.shape)
Output:
In this example, we have used the same one-dimensional array ‘a’, and using the numpy newaxis function expanded its dimension into a two-dimensional array and we have converted the shape from (3, ) to (3,1). The position of the np.newaxis object is very important in determining the shape of the array. Here we have declared the newaxis object in the 1st axis.
Example #6
Code:
import numpy as np
a=np.array([[2, 4, 6], [4, 8, 12]])
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
c=np.array(a)[np.newaxis,:,:]
print('array: \n', c)
print('array.ndim:', c.ndim)
print('array.shape:', c.shape)
Output:
In this example the two-dimensional array ‘a’ with the shape of (2,3) has been converted into a 3-dimensional array with a shape of (1,2,3) this is possible by declaring the numpy newaxis function along the 0th axis and declaring the semicolon representing the array dimension to (1,2,3). By using this technique, we can convert any numpy array to our desired shape and dimension.
Example #7
Code:
import numpy as np
a=np.array([[2, 4, 6], [4, 8, 12]])
print('array: \n', a)
print('array.ndim:', a.ndim)
print('array.shape:', a.shape)
c=np.array(a)[np.newaxis,:,:]
d=np.array(a)[:, np.newaxis, :]
e=np.array(a)[:, :, np.newaxis]
print('array: \n', c)
print('array.ndim:', c.ndim)
print('array.shape:', c.shape)
print('array: \n', d)
print('array.ndim:', d.ndim)
print('array.shape:', d.shape)
print('array: \n', e)
print('array.ndim:', e.ndim)
print('array.shape:', e.shape)
Output:
Similar to the previous example using the one-dimensional array ‘a’ we have converted three-dimensional array ‘c’, ‘d’ & ‘e’. All three resulting arrays are three dimensions with different shapes namely (1,2,3), (2,1,3) & (2,3,1) which show us the position of our newaxis object we have declared along with the array. For array ‘c’ we have declared the newaxis object at the 1st position, for the array ‘d’ we have declared the newaxis object at the 2nd position and for the array ‘e’ we have declared the newaxis object at the 3rd position which gives the corresponding shapes.
Conclusion
In this article, we have discussed the Numpy newaxis function in detail using various examples to get a clear understanding of the numpy newaxis function and its uses. We have also discussed in detail how to use the newaxis object inside the tuple to convert the array into multi-dimensional arrays and techniques involved in changing the shape of the resulting array with examples. I hope this article helps. Thank you.
Recommended Articles
This is a guide to NumPy Newaxis. Here we discuss the Numpy newaxis function in detail using various examples to get a clear understanding of the numpy newaxis function. You may also have a look at the following articles to learn more –