Bird
0
0

Given a 2D NumPy array arr = np.array([[1, 2, 3], [4, 5, 6]]), which code correctly extracts the second column as a 1D array?

hard📝 Application Q15 of 15
SciPy - Basics and Scientific Computing
Given a 2D NumPy array arr = np.array([[1, 2, 3], [4, 5, 6]]), which code correctly extracts the second column as a 1D array?
Aarr[:, 2]
Barr[1, :]
Carr[1]
Darr[:, 1]
Step-by-Step Solution
Solution:
  1. Step 1: Understand array slicing for columns

    To get a column, select all rows (:) and the column index.
  2. Step 2: Identify correct column index

    Second column has index 1 (zero-based indexing).
  3. Step 3: Check options

    arr[:, 1] uses arr[:, 1] which selects all rows, column 1 correctly.
  4. Final Answer:

    arr[:, 1] -> Option D
  5. Quick Check:

    All rows, column 1 = arr[:, 1] [OK]
Quick Trick: Use arr[:, col_index] to get a column [OK]
Common Mistakes:
MISTAKES
  • Using arr[1, :] which selects a row, not a column
  • Using arr[1] which selects second row
  • Using wrong column index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes