Gather Elements at Row and Column Index Pairs with NumPy Fancy Indexing

Quick Overview

Write a NumPy function that returns the elements of a 2D array at a list of (row, column) coordinate pairs without a Python loop. It tests advanced integer indexing, how index arrays pair up and broadcast, and the pitfalls of repeated coordinates.

Gather Elements at Row and Column Index Pairs with NumPy Fancy Indexing

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `elements_at(a, rows, cols)`. Given a 2-D array `a` and equal-length 1-D integer arrays `rows` and `cols`, return a 1-D array whose element `k` is `a[rows[k], cols[k]]`. Requirements: - Use paired fancy indexing: `a[rows, cols]`. - Result is 1-D with the same length as `rows` and `cols`. - No Python loops or comprehensions over the index pairs. For example: ```python a = np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18]]) rows = np.array([0, 2, 1]) cols = np.array([1, 2, 0]) elements_at(a, rows, cols) # array([11, 18, 13]) # a[0,1], a[2,2], a[1,0] ``` ```hint Compare the shapes Indexing rows first and columns second, in two steps, returns something of a different shape. Work out what each form gives on the example before you commit. ``` ### What a Strong Answer Covers - One indexing expression that returns a 1-D array of length K, checked against the example. - How NumPy combines two integer index arrays, and how that differs from selecting a submatrix. - Whether the result is a view or a copy, and the time and memory cost. - Edge cases: index arrays of unequal length, negative or out-of-range indices, and index arrays with a non-integer dtype. ### Follow-up Questions - How does this differ from `a[rows][:, cols]`? - How would you add a vector of values back into `a` at these coordinates when a coordinate repeats? - How would you get the full submatrix of every listed row against every listed column? - Given a matrix of predicted class probabilities and a vector of true labels, how would you pull out each sample's probability for its true class?

Overview: Write a NumPy function that returns the elements of a 2D array at a list of (row, column) coordinate pairs without a Python loop. It tests advanced integer indexing, how index arrays pair up and broadcast, and the pitfalls of repeated coordinates.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write elements_at(a, rows, cols). Given a 2-D array a and equal-length 1-D integer arrays rows and cols, return a 1-D array whose element k is a[rows[k], cols[k]].

Requirements:

  • Use paired fancy indexing: a[rows, cols] .
  • Result is 1-D with the same length as rows and cols .
  • No Python loops or comprehensions over the index pairs.

For example:

a = np.array([[10, 11, 12],
              [13, 14, 15],
              [16, 17, 18]])
rows = np.array([0, 2, 1])
cols = np.array([1, 2, 0])
elements_at(a, rows, cols)
# array([11, 18, 13])   # a[0,1], a[2,2], a[1,0]

What a Strong Answer Covers Guidance

  • One indexing expression that returns a 1-D array of length K, checked against the example.
  • How NumPy combines two integer index arrays, and how that differs from selecting a submatrix.
  • Whether the result is a view or a copy, and the time and memory cost.
  • Edge cases: index arrays of unequal length, negative or out-of-range indices, and index arrays with a non-integer dtype.

Follow-up Questions Guidance

  • How does this differ from a[rows][:, cols] ?
  • How would you add a vector of values back into a at these coordinates when a coordinate repeats?
  • How would you get the full submatrix of every listed row against every listed column?
  • Given a matrix of predicted class probabilities and a vector of true labels, how would you pull out each sample's probability for its true class?