In [ ]:
from IPython.display import display_pretty
import numpy as np

indexing

  • common single element indexing
  • complex indexing
  • index arrays
  • Boolean or “mask” index arrays
  • Structural indexing tools
  • Assigning values to indexed arrays
  • Dealing with variable numbers of indices within programs

In [ ]:
a = np.arange(35).reshape(5,7)
b = a[1:5:2, ::3]
c = a[[0, 2, 4], [0, 1, 2]]
d = a[[0, 2, 4], 1]
x = np.arange(10, 0, -1)
e = x[np.array([[1, 5], [4, 6]])]
display_pretty(b ,c, d, e)

# boolean/mask indexing ways
f = x[x>5]
display_pretty(f)

g = a > 20
h = g[:, 5]
i = a[h]
display_pretty(g, h, i)

Structural indexing tools


In [ ]:
x = np.arange(0, 5)
y1 = x[:, np.newaxis] + x[np.newaxis, :]
x = np.arange(81).reshape([3, 3, 3, 3])
y2 = x[1, :, :, 2]
display_pretty(y1, y2)

Assigning values to indexed arrays


In [ ]:
x = np.arange(10, 50, 10)
x[np.array([1, 1, 3, 1])] += 1
x

Dealing with variable numbers of indices within programs


In [ ]:
x = np.arange(81).reshape([3, 3, 3, 3])
y1 = x[1, 1, 1, 1]
y2 = x[[1, 1, 1, 1]]
display_pretty(y1, y2)