#2 - NumPy Indexing, Selection & Operations
Before we start, I would like you to go through the following article.
Indexing in NumPy
Array indexing is the same as accessing an array element. It simply means that you can get access to any element in the array just by their index. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Indexing in 1D Array
- Let's first create an array.
import numpy as np
arr = np.arange(0,11)
print(arr)
The indexing in NumPy is somehow similar to that of Python inbuild indexing, So if we want to find the element at the index 8 we can use -> print(arr[8]) which will give the output as 8.
If you want to get the output in a range we can use the slicing notation, like if you want to find out elements between index 1 and 5 then we can use -> print(arr[1:5]). Here we can remove the start or and endpoint just to find out all the elements before the endpoint or after the start point just like we do in LISTS.
Broadcast in NumPy -> what it does is, it sets the number of values at a range of index to what you have assigned it to be. The point here that needs to be noted is that, if we are making any changes to the copied broadcasted elements then it will affect the original array. This happens because NumPy doesn't set copies to the original arrays. If you want to take a copy we need to use the copy method arr_cpy = arr.copy()
import numpy as np
arr = np.arange(0,11)
arr[0:5] = 20
print(arr)
# OUTPUT - [20 20 20 20 20 5 6 7 8 9 10]
import numpy as np
arr = np.arange(0,11)
slice = arr[0:5]
slice[:] = 20
print(slice)
print(arr)
#OUTPUT will be
#[20 20 20 20 20]
#[20 20 20 20 20 5 6 7 8 9 10]
# This happens because NumPy doesn't set copies to the original arrays.
Indexing in 2D Array
- Let's start with creating a 2D array
import numpy as np
arr_2d = np.array([[43,34,5],[67,23,89],[84,45,67]])
print(arr_2d)
- Now in order to capture an element, we can make use of the double bracket notation or the single bracket notation.
import numpy as np
arr_2d = np.array([[43,34,5],[67,23,89],[84,45,67]])
print(arr_2d )
print (arr_2d[0][2])
# OR
print(arr_2d[0,2])
- The output will be->
[[43 34 5]
[67 23 89]
[84 45 67]]
5
5
Now assume you want a submatrix from the original matrix. We can make use of ':' this is also known as the slice notation. arr_2d[:2,1:]
Conditional selection
If we compare a list of an array with a conditional statement then we will be provided with a set of boolean values.
import numpy as np
arr = np.arange(1,11)
print(arr > 5)
#OUTPUT ->
# [False False False False False True True True True True]
- Now we can use this for conditional selection, to find out all the values greater than 5
import numpy as np
arr = np.arange(1,11)
bool_arr = arr > 5
print(arr[bool_arr])
# Or -> print(arr[arr>5])
#OUTPUT ->
#[ 6 7 8 9 10]
- Now you can go ahead and play around with Indexing and selection.
NumPy Operations
Array with Array operations
- We can simply perform an array with array operations just by using the arithmetic signs. Like if you want to add two arrays together on the element by element basis we can do the following.
import numpy as np
arr = np.arange(0,11)
double = arr + arr
print(double)
#OUTPUT -> [ 0 2 4 6 8 10 12 14 16 18 20]
- Above we have seen addition similarly we can have multiplication (*) , subtraction (-) or division (/) , (//) . The length of the arrays should be the same else it will give an error message.
Universal Array Funtions
NumPy comes with many universal array functions which are just mathematical functions that you can perform on the array itself and broadcast across the array itself.
to the square root we can make use of np.sqrt(arr).
- If you want to find the exponential then we would make use of np.exp(arr)
- for maximum and minimum we would make use of np.max(arr) or np.min(arr)
- you can even perform trigonometric functions in NumPy, like np.sin(arr) this will pass every value in sin. Similarly, we can perform the same very other trigonometric functions.
- we can take the log as well using np.log(arr) since the log(o) is infinity then you might receive a runtime warning for the same.
- for reference, you can find all the Universal functions here.
Thank-you!
I am glad you made it to the end of this article. I hope you got to learn something, if so please leave a Like which will encourage me for my upcoming write-ups.
- My GitHub Repos
- Connect with me on Linkedin
- Start your own blogs