NumPy provides powerful indexing capabilities for arrays. Indexing capabilities in NumPy became so popular that many of them were added back to Python.
Indexing NumPy arrays, in many ways, is very similar to indexing lists or tuples. There are some differences, which will become apparent as we proceed. To start with, let's create an array that has 100 x 100 dimensions:
In [9]: x = np.random.random((100, 100))
Simple integer indexing works by typing indices within a pair of square brackets and placing this next to the array variable. This is a widely used Python construct. Any object that has a __getitem__
method will respond to such indexing. Thus, to access the element in the 42nd row and 87th column, just type this:
In [10]: y = x[42, 87]
Like lists and other Python sequences, the use of a colon to index a range of values is also supported. The following statement will print the k
th row of the x
matrix.
In [11]: print(x[k, :])
The colon can be...