Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

NumPy - Indexing & Slicing



Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python's in-built container objects.

NumPy Indexing

NumPy Indexing is used to access or modify elements in an array. Three types of indexing methods are available field access, basic slicing and advanced indexing.

Example 1

In the below example, we have created an array using arange() function and let us see how to access a single element from the array i.e, 6.

import numpy as np  
a = np.arange(10) 
b = a[6] 
print(b)

Following is an output of the above code −

6

Example 2

Let's say we have a list which contains 5 student marks in English and we need to access the score of third student, we use arr[2] as the index starts from '0'.

import numpy as np
scores = ['86', '98', '100', '65', '75']
arr = np.array(scores)
print("Third student score is:", arr[2])

Following is an output of the above code −

Third student score is: 100

Slicing in NumPy

NumPy Slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.

Example 1

In the below code we will see how to access last two elements from the array using arr[-2:] as we didn't specify the stop parameter it access the elements from the second last to the end of the array.

import numpy as np
arr = np.arange(6)
print(arr[-2:])

Following is an output of the above code −

[4 5]

Example 2

Let's say we have an array containing numbers 1 to 12 and need to access only even numbers, we use slicing with step parameter 'arr[::2]' as it slices every second element in the array.

import numpy as np
arr = np.arange(12)
even_num = arr[::2]
print("Even Numbers:", even_num)

Following is an output of the above code −

Even Numbers: [ 0  2  4  6  8 10]

Example 3

Let's create a 2D array and use slicing to access second column in the array. To access all rows (:) but only the second column (index 1) we use arr[:, 1]

import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
print(arr[:, 1])

Following is an output of the above code −

[20 50 80]

Example 4

In the below code we have created a 2D array and let us see how to access all elements from row 2 (index 1) & minus; we use a[1:]. Where, a[1:] which selects all rows starting from the second row (index 1) to the last, including all columns.

import numpy as np 
a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 
print (a)  
# slice items starting from index
print ('Now we will slice the array from the index a[1:]')
print (a[1:])

Following is an output of the above code −

[[1 2 3]
 [3 4 5]
 [4 5 6]]

Now we will slice the array from the index a[1:]
[[3 4 5]
 [4 5 6]]

Example 5

Let us see how to slice an array between indexes −

import numpy as np
a = np.arange(10)
print("Array from index 1 to 6:", a[1:7])

When we run above program, it produces following result −

Array from index 1 to 6: [1 2 3 4 5 6]
Advertisements