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.
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.
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[5]
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.
x[-3]
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.
x.shape = (5,2)
x
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.
x[3,1]
7
[adrotate banner=”3″]
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
x = np.arange(10)
x
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.
x[1:6]
array([1, 2, 3, 4, 5])
Example 2: Using multidimensional array for performing slicing
y = np.arange(25)
y
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])
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
y = np.arange(25).reshape(5,5)
y
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
y[3:4,1:2]
array([[16]])
Example 3: Using Negative indexing for slicing
Here we have used multidimensional arrays for slicing through negative indexing
x = np.array([[0, 1, 2],[3, 4, 5], [6, 7, 8],[5,2,6]])
x
array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [5, 2, 6]])
Here -2 index fetches 2nd row and its elements are printed.
x[-2]
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()
a = np.arange(10)
a
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.
np.where(a < 6, a, 9*a)
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/
- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal
- Also Read – Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python