Bird
0
0

You have a 1D numpy array data with shape (100,). You want to prepare it for a machine learning model that expects input shape (100, 1). Which code correctly reshapes data using np.newaxis?

hard📝 Application Q8 of 15
NumPy - Array Manipulation
You have a 1D numpy array data with shape (100,). You want to prepare it for a machine learning model that expects input shape (100, 1). Which code correctly reshapes data using np.newaxis?
Adata[np.newaxis, :]
Bdata[np.newaxis]
Cdata[:, :, np.newaxis]
Ddata[:, np.newaxis]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the target shape

    The model expects shape (100, 1), meaning 100 rows and 1 column.
  2. Step 2: Use np.newaxis to add column dimension

    Adding np.newaxis after ':' adds a new axis as the second dimension, resulting in shape (100, 1).
  3. Final Answer:

    data[:, np.newaxis] -> Option D
  4. Quick Check:

    New axis as second dimension = data[:, np.newaxis] [OK]
Quick Trick: Add np.newaxis after ':' to add a column dimension [OK]
Common Mistakes:
  • Adding newaxis at front
  • Using too many indices
  • Not matching expected shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes