...

Web Technologies - NumPy

Back to Course

Lesson Description


Lession - #306 NumPy Array Filter


Filtering Arrays

Getting a few components out of a current array and making another array out of them is called filtering.
In NumPy, you filter an array utilizing a boolean index list.
Assuming the value at an index is True that component is contained in the filtered array, on the off chance that the value at that index is False that component is barred from the filtered array.

Example
Create an array from the elements on index 0 and 2:
import numpy as np

arr = np.array([41, 42, 43, 44]>
x = [True, False, True, False] newarr = arr[x] print(newarr>


Creating the Filter Array

In the example above we hard-coded the True and False values, however the normal use is to make a filter array in view of conditions.

Example
Make a filter array that will return just values higher than 42:
import numpy as np

arr = np.array([41, 42, 43, 44]>
# Create an empty list filter_arr = [] # go through each element in arr for element in arr: # if the element is higher than 42, set the value to True, otherwise False: if element > 42: filter_arr.append(True>
else: filter_arr.append(False>
newarr = arr[filter_arr] print(filter_arr>
print(newarr>


Creating Filter Directly From Array

We can straightforwardly substitute the array rather than the iterable variable in our condition and it will work similarly as we anticipate that it should.

Example
Make a filter array that will return just values higher than 42:
import numpy as np

arr = np.array([41, 42, 43, 44]>
filter_arr = arr > 42 newarr = arr[filter_arr] print(filter_arr>
print(newarr>







What does NumPy library contains? NumPy contains a multi-dimensional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines.
numpy logspace ... Return numbers spaced evenly on a log scale
NumPy slicing ... The start parameter represents the starting index, end is the ending index, and step is the number of items that are "stepped" over.
numpy add(>
function is used when we want to compute the addition of two array. It add arguments element-wise.