numpy array index code example

Example 1: indexing a numpy array in python

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 25 26 27 28 29]
  [30 31 32 33 34 35]]
array[0, 3:5]  =  [3 4]

array[4:, 4:] = [[28 29],
             [34 35]]

array[:, 2] =  [2 8 14 20 26 32]

array[2:;2, ::2] = [[12 14 16],
                [24 26 28]]

Example 2: how to index an array in python

arrayName[Index Number]

Example 3: python numpy array boolean indexing

x = np.array([1., -1., -2., 3])
x[x < 0] = 20
x

Output:
array([  1.,  20.,  20.,   3.])

Example 4: numpy indexing

>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[1:7:2]
array([1, 3, 5])

Example 5: how do i index into array

>>> x = np.arange(10)
>>> x[2]
2
>>> x[-2]
8

Tags:

Misc Example