Bird
0
0

Which of the following is the correct syntax to add a new axis to a 1D numpy array a to make it a column vector?

easy📝 Syntax Q12 of 15
NumPy - Array Manipulation
Which of the following is the correct syntax to add a new axis to a 1D numpy array a to make it a column vector?
Aa[:, np.newaxis]
Ba[np.newaxis, :]
Ca[np.newaxis]
Da[:, :np.newaxis]
Step-by-Step Solution
Solution:
  1. Step 1: Understand array shape and axis addition

    To convert a 1D array to a column vector, we add a new axis as the second dimension using a[:, np.newaxis].
  2. Step 2: Check each option

    a[np.newaxis, :] adds a new axis at the front, making a row vector (1, 3). a[np.newaxis] does the same, making a row vector (1, 3). a[:, :np.newaxis] runs but does not add a dimension since :np.newaxis is equivalent to :, keeping shape (3,).
  3. Final Answer:

    a[:, np.newaxis] -> Option A
  4. Quick Check:

    Column vector = a[:, np.newaxis] [OK]
Quick Trick: Use a[:, np.newaxis] to add a column dimension [OK]
Common Mistakes:
  • Using np.newaxis in wrong position
  • Confusing row and column vector shapes
  • Syntax errors like a[:, :np.newaxis]

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes