Quantcast
Channel: Teaching Software Carpentry » Joshua Adelman
Viewing all articles
Browse latest Browse all 6

Assessment Questions: Numpy Arrays

$
0
0

Novice from Competent

Given the 2D array generated using the following code snippet:

———————————-
import numpy as np
x = np.random.normal(size=(10,10))
y = x[2:5,1:5]

———————————-

What is the shape of y?

  1. (4,5)
  2. (3,3)
  3. (3,4)
  4. (5,4)
  5. (4,3)

Expert from Competent

Suppose you had an array generated by the following code:

x = np.arange(25).reshape((5,5))

array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])

Which of the following commands would give you the following array?

array([[ 1, 4],
[ 6, 9],
[21, 24]])

  1. x[[0,1,4],[1,4]]
  2. x[np.ix_([0,1,4],[1,4])]
  3. x[np.meshgrid([0,1,4],[1,4])]
  4. x[[0,1,4]][1,4]
  5. x[[0,1,4]][[1,4],:]

Viewing all articles
Browse latest Browse all 6

Trending Articles