Tutorial – Numpy Indexing, Numpy Slicing, Numpy Where in Python

Tutorial - Numpy Indexing, Numpy Slicing, Numpy Where in Python
Numpy Indexing, Numpy Slicing, Numpy Where

Introduction

In your machine learning or data science projects you would often be required to fetch specific elements of the numpy array or slice its content. There are functions of numpy indexing, numpy slicing, and numpy where to carry out such operations. Throughout this tutorial, we will try to learn about these functions with the help of examples where we will also understand the syntax of these numpy functions.

Importing Numpy Library

Before we begin, let’s just import numpy library.

In [1]:
import numpy as np

We are starting this article by first looking at indexing operation.

Numpy Indexing

Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to perform indexing.

Example 1: Single element indexing

This example shows how we can access specific values from an array.

Ad
Deep Learning Specialization on Coursera
In [2]:
x = np.arange(10)
x
Out[2]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [3]:
x[5]
Out[3]:
5

Example 2: Using negative indexing

The negative indexing starts from last value and ends at first value. The negative indexing starts from 9 which -1th index. This is the reason -3rd index results in 7.

In [4]:
x[-3]
Out[4]:
7

Example 3: Multidimensional Indexing

Numpy arrays support multidimensional indexing for multidimensional arrays. Here using shape attribute, we have converted the flattened 1-D array into an array of 5 rows and 2 columns.

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

While indexing we can fetch different values, here in this example we have accessed value at 3rd row and 1st column.

NOTE – The indexing in array starts from 0th index.

In [6]:
x[3,1]
Out[6]:
7

The next operation is slicing.

Numpy Slicing

It is possible to slice arrays to extract arrays of the same number of dimensions, but of different sizes than the original.

Example 1: Using single dimension array for slicing

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

Here for slicing, we specify the starting value which is 1 in this case and ending value is 6. The result of slicing is again an array where starting value is included and ending value is excluded from the result.

In [8]:
x[1:6]
Out[8]:
array([1, 2, 3, 4, 5])

Example 2: Using multidimensional array for performing slicing

In [9]:
y = np.arange(25)
y
Out[9]:
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])
In [10]:
We have used reshape method to convert the shape of array to 5 x 5. 
  File "<ipython-input-10-6b7309d62191>", line 1
    We have used reshape method to convert the shape of array to 5 x 5.
          ^
SyntaxError: invalid syntax
In [11]:
y = np.arange(25).reshape(5,5)
y
Out[11]:
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]])

Using the slicing method, we have fetched the result as 16 by specifying row index as 3rd and column index as 1st

In [12]:
y[3:4,1:2]
Out[12]:
array([[16]])

Example 3: Using Negative indexing for slicing

Here we have used multidimensional arrays for slicing through negative indexing

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

Here -2 index fetches 2nd row and its elements are printed.

In [14]:
x[-2]
Out[14]:
array([6, 7, 8])

Lastly, we have numpy where operation.

Numpy Where: np.where()

Numpy where function is used for executing an operation on the fulfillment of a condition.

Syntax

numpy.where(condition[x,y])

condition : array_like,bool – This results either x if true is obtained otherwise y is yielded if false is obtained.

x,y : array_like – These are the values from which to choose. x, y and condition need to be broadcastable to some shape.

Output of the numpy where() is an array with elements from x where condition is True, and elements from y elsewhere.

Example 1 : example of np.where()

In [15]:
a = np.arange(10)
a
Out[15]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Here the condition states that all the values which are lesser than 6 will be printed without any changes and values greater than 6 will be multiplied with 9. This is the reason we can see that values greater than 5 are changed.

In [16]:
np.where(a < 6, a, 9*a)
Out[16]:
array([ 0,  1,  2,  3,  4,  5, 54, 63, 72, 81])

Conclusion

Here in this tutorial we learned about the most fundamental operations performed on arrays. We explored numpy indexing, numpy slicing, and numpy where operations. It is highly recommended to be proficient with these functions of array.

 

Reference-  https://numpy.org/doc/

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet