0
0
NumPydata~5 mins

np.newaxis for adding dimensions in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.newaxis do in NumPy?

np.newaxis adds a new dimension to an array, increasing its rank by one. It is used to change the shape of arrays without copying data.

Click to reveal answer
beginner
How would you convert a 1D array of shape (5,) to a 2D column vector using np.newaxis?

Use array[:, np.newaxis]. This adds a new axis as the second dimension, changing shape from (5,) to (5, 1).

Click to reveal answer
beginner
Given arr = np.array([1, 2, 3]), what is the shape of arr[np.newaxis, :]?

The shape becomes (1, 3). A new axis is added at the front, turning the 1D array into a 2D row vector.

Click to reveal answer
intermediate
Why is np.newaxis useful when working with broadcasting in NumPy?

It helps align array dimensions by adding new axes, so arrays can broadcast correctly during operations like addition or multiplication.

Click to reveal answer
intermediate
Can np.newaxis be used multiple times on the same array? What does it do?

Yes, each use adds one new dimension. For example, arr[np.newaxis, :, np.newaxis] adds two new axes, increasing the array's rank by two.

Click to reveal answer
What shape does np.array([1,2,3])[np.newaxis, :] have?
A(3, 1)
B(1,)
C(3,)
D(1, 3)
Which of these adds a new axis as the last dimension to a 1D array arr?
Aarr[:, np.newaxis]
Barr[np.newaxis, :]
Carr[np.newaxis, np.newaxis]
Darr[:, :]
Why might you use np.newaxis before performing element-wise multiplication of arrays?
ATo align dimensions for broadcasting
BTo copy the array
CTo increase array size
DTo flatten the array
If arr has shape (4,), what is the shape of arr[np.newaxis, np.newaxis, :]?
A(1, 4, 1)
B(1, 1, 4)
C(4, 1, 1)
D(4, 4, 4)
Which statement about np.newaxis is FALSE?
AIt changes array shape
BIt adds a new dimension
CIt copies the array data
DIt helps with broadcasting
Explain how np.newaxis changes the shape of a 1D NumPy array and why this might be useful.
Think about turning a list into a row or column.
You got /4 concepts.
    Describe a real-life example where adding a new axis with np.newaxis helps in data analysis or machine learning.
    Consider how images or features might need reshaping.
    You got /4 concepts.