Numpy Practice 13/18

Screen Link:

My Code:

def copy_row_k_times(x, row_index, num_copies):
    return x[[row_index] * num_copies]

Please can someone explain the logic behind the solution provided above?


First, break the problem down.

  • What is [row_index]?
  • What would the output of [row_index] * num_copies be?
  • What would the output of x[row_index] be?
  • What would the output of x[[row_index]] be?
  • What would the output of x[[row_index] * num_copies] be?

Think about each individual component, print out values if you can’t figure something out, then think about how it all fits in in relation to indexing. Feel free to ask specific questions if you get stuck.

The intuition behind the operation of duplicating rows is called “broadcasting.”

You can read up on the following