Indexing Multidimensional Arrays¶
In [2]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [3]:
bar = np.array([[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]])
bar
Out[3]:
array([[ 5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]])
In [5]:
bar[1,2]
Out[5]:
35
In [6]:
bar[0]
Out[6]:
array([ 5, 10, 15, 20])
In [7]:
bar[0, None]
Out[7]:
array([[ 5, 10, 15, 20]])
In [8]:
bar[:1]
Out[8]:
array([[ 5, 10, 15, 20]])
In [10]:
bar[1:3, [-2, -1]]
Out[10]:
array([[35, 40], [55, 60]])
In [12]:
bar[0][0] = -1
bar
Out[12]:
array([[-1, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]])
In [14]:
bar[1] = bar[2]
bar
Out[14]:
array([[-1, 10, 15, 20], [45, 50, 55, 60], [45, 50, 55, 60]])
In [16]:
bar[[0, 1, 2], [0, 1, 2]] = 0
bar
Out[16]:
array([[ 0, 10, 15, 20], [45, 0, 55, 60], [45, 50, 0, 60]])
In [18]:
zoo = np.random.randint(low=0, high=10, size=(2,3,2))
zoo
Out[18]:
array([[[5, 3], [2, 5], [4, 4]], [[7, 6], [8, 6], [9, 9]]])
In [20]:
zoo[0, :, 1] = 5
zoo
Out[20]:
array([[[5, 5], [2, 5], [4, 5]], [[7, 6], [8, 6], [9, 9]]])