Python numpy array filtering

Как отфильтровать массив NumPy (4 примера)

Вы можете использовать следующие методы для фильтрации значений в массиве NumPy:

Метод 1: фильтрация значений на основе одного условия

#filter for values less than 5 my_array[my_array < 5 ] 

Метод 2: фильтрация значений с использованием условия «ИЛИ»

#filter for values less than 5 *or* greater than 9 my_array[(my_array < 5 ) | (my_array >9 )] 

Способ 3: фильтрация значений с использованием условия «И»

#filter for values greater than 5 *and* less than 9 my_array[(my_array > 5 ) & (my_array < 9 )] 

Способ 4: фильтрация значений, содержащихся в списке

#filter for values that are equal to 2, 3, 5, or 12 my_array[np.in1d (my_array, [2, 3, 5, 12])] 

В этом руководстве объясняется, как использовать каждый метод на практике со следующим массивом NumPy:

import numpy as np #create NumPy array my_array = np.array([1, 2, 2, 3, 5, 6, 7, 10, 12, 14]) #view NumPy array my_array array([ 1, 2, 2, 3, 5, 6, 7, 10, 12, 14]) 

Пример 1. Фильтрация значений по одному условию

В следующем коде показано, как фильтровать значения в массиве NumPy на основе всего одного условия:

#filter for values less than 5 my_array[(my_array < 5 )] array([1, 2, 2, 3]) #filter for values greater than 5 my_array[(my_array >5 )] array([ 6, 7, 10, 12, 14]) #filter for values equal to 5 my_array[(my_array == 5 )] array([5]) 

Пример 2. Фильтрация значений с использованием условия «ИЛИ»

В следующем коде показано, как фильтровать значения в массиве NumPy с помощью условия «ИЛИ»:

#filter for values less than 5 *or* greater than 9 my_array[(my_array < 5 ) | (my_array >9 )] array([ 1, 2, 2, 3, 10, 12, 14]) 

Этот фильтр возвращает значения в массиве NumPy, которые меньше 5 или больше 9.

Пример 3. Фильтрация значений с использованием условия «И»

В следующем коде показано, как фильтровать значения в массиве NumPy с помощью условия «И»:

#filter for values greater than 5 *and* less than 9 my_array[(my_array > 5 ) & (my_array < 9 )] array([6, 7]) 

Этот фильтр возвращает значения в массиве NumPy, которые больше 5 и меньше 9.

Пример 4: значения фильтра, содержащиеся в списке

В следующем коде показано, как фильтровать значения в массиве NumPy, содержащиеся в списке:

#filter for values that are equal to 2, 3, 5, or 12 my_array[np.in1d (my_array, [2, 3, 5, 12])] array([ 2, 2, 3, 5, 12]) 

Этот фильтр возвращает только значения, равные 2, 3, 5 или 12.

Примечание.Полную документацию по функции NumPy in1d() можно найти здесь .

Дополнительные ресурсы

В следующих руководствах объясняется, как выполнять другие распространенные операции фильтрации в Python:

Источник

Filter a Numpy Array – With Examples

In this tutorial, we will look at how to filter a numpy array.

How to filter numpy arrays?

Filtering a numpy array - illustration

You can filter a numpy array by creating a list or an array of boolean values indicative of whether or not to keep the element in the corresponding array. This method is called boolean mask slicing. For example, if you filter the array [1, 2, 3] with the boolean list [True, False, True] , the filtered array would be [1, 3] .

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The following is the syntax to filter a numpy array using this method –

# arr is a numpy array # boolean array of which elements to keep, here elements less than 4 mask = arr < 4 # filter the array arr_filtered = arr[mask] # above filtering in a single line arr_filtered = arr[arr < 4]

Alternatively, you can also use np.where() to get the indexes of the elements to keep and filter the numpy array based on those indexes. The following is the syntax –

# arr is a numpy array # indexes to keep based on the condition, here elements less than 4 indexes_to_keep = np.where(arr < 4) # filter the array arr_filtered = arr[indexes_to_keep] # above filtering in a single line arr_filtered = arr[np.where(arr < 4)]

Examples

Let’s look at some examples to better understand the usage of the above methods for different use-cases.

First, we will create a numpy array that we will be using throughout this tutorial –

import numpy as np # create a numpy array arr = np.array([1, 4, 2, 7, 9, 3, 5, 8]) # print the array print(arr)

1. Filter array based on a single condition

Let’s filter the above array arr on a single condition, say elements greater than 5 using the boolean masking method.

# boolean mask of elements to keep mask = arr > 5 print(mask) # filter the array arr_filtered = arr[mask] # show the filtered array print(arr_filtered)
[False False False True True False False True] [7 9 8]

You can see that we printed the boolean mask and the filtered array. Masking and filtering can be done in a single line –

# filter array filtered_arr = arr[arr > 5] print(filtered_arr)

Let’s now go ahead and perform the same filtering, this time using np.where() instead of a boolean list or array.

# indexes of elements to keep indexes_to_keep = np.where(arr > 5) print(indexes_to_keep) # filter the array arr_filtered = arr[indexes_to_keep] # show the filtered array print(arr_filtered)
(array([3, 4, 7], dtype=int64),) [7 9 8]

The indexes of elements to keep is printed followed by the filtered array. The np.where() function gives us the indexes satisfying the condition which are then used to filter the array. A shorter version of the above code is –

# filter array filtered_arr = arr[np.where(arr > 5)] print(filtered_arr)

2. Filter array based on two conditions

To filter the array on multiple conditions, you can combine the conditions together using parenthesis () and the “and” & operator – ((condition1) & (condition2) & . )

Let’s filter the array “arr” on two conditions – greater than 5 and less than 9 using boolean masking.

# filter array filtered_arr = arr[(arr > 5) & (arr < 9)] print(filtered_arr)

The returned array only contains elements from the original array that are greater than 5 and less than 9, satisfying both the conditions.

Let’s now perform the same filtering using np.where() –

# filter array filtered_arr = arr[np.where(((arr > 5) & (arr < 9)))] print(filtered_arr)

We get the same result satisfying both the conditions.

In the above examples, we filtered the array on two conditions but this method can easily be extended to multiple conditions.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

  • How to sort a Numpy Array?
  • Create Pandas DataFrame from a Numpy Array
  • Different ways to Create NumPy Arrays
  • Convert Numpy array to a List – With Examples
  • Append Values to a Numpy Array
  • Find Index of Element in Numpy Array
  • Read CSV file as NumPy Array
  • Filter a Numpy Array – With Examples
  • Python – Randomly select value from a list
  • Numpy – Sum of Values in Array
  • Numpy – Elementwise sum of two arrays
  • Numpy – Elementwise multiplication of two arrays
  • Using the numpy linspace() method
  • Using numpy vstack() to vertically stack arrays
  • Numpy logspace() – Usage and Examples
  • Using the numpy arange() method
  • Using numpy hstack() to horizontally stack arrays
  • Trim zeros from a numpy array in Python
  • Get unique values and counts in a numpy array
  • Horizontally split numpy array with hsplit()

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

NumPy Filter Array

Getting some elements out of an existing array and creating a new array out of them is called filtering.

In NumPy, you filter an array using a boolean index list.

A boolean index list is a list of booleans corresponding to indexes in the array.

If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

Example

Create an array from the elements on index 0 and 2:

The example above will return [41, 43] , why?

Because the new array contains only the values where the filter array had the value True , in this case, index 0 and 2.

Creating the Filter Array

In the example above we hard-coded the True and False values, but the common use is to create a filter array based on conditions.

Example

Create a filter array that will return only values higher than 42:

# 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)

Example

Create a filter array that will return only even elements from the original array:

arr = np.array([1, 2, 3, 4, 5, 6, 7])

# Create an empty list
filter_arr = []

# go through each element in arr
for element in arr:
# if the element is completely divisble by 2, set the value to True, otherwise False
if element % 2 == 0:
filter_arr.append(True)
else:
filter_arr.append(False)

Creating Filter Directly From Array

The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it.

We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to.

Example

Create a filter array that will return only values higher than 42:

Example

Create a filter array that will return only even elements from the original array:

arr = np.array([1, 2, 3, 4, 5, 6, 7])

Источник

Читайте также:  Update sql with java
Оцените статью