Assessment questions: NumPy Arrays

Oct 28, 2013 • David Jones

Here is my attempt.

Novice/Competent

xs is a numpy array.  Which command sets all but the first and last values in xs to 0?

  • A – xs[0:-2] = 0
  • B –  xs[1:-2] = 0
  • C – xs[0:-1] = 0
  • D –  xs[1:-1] = 0

Competent/Expert

ys is a numpy array with shape (3,2,3)

xs is another numpy array, and we would like to add them by:

zs = xs + ys

Which one of the following shapes is valid for xs?

  • A – (3,2)
  • B – (3,2,1)
  • C – (3,3,3)
  • D – (3,2,3,3)