NumPy – 按多个条件过滤行
在本文中,我们将讨论如何按多个条件过滤 NumPy 数组的行。在开始按多个条件过滤行之前,让我们先看看如何基于一个条件应用过滤器。基本上有两种方法可以做到这一点:
方法一:使用掩码数组
掩码函数从数组arr 中过滤出掩码数组中位于 false 索引处的数字。开发者可以根据自己的需要设置掩码数组——当很难形成过滤逻辑时,它会变得非常有用。
方法
- 导入模块
- 制作初始数组
- 定义掩码
- 根据掩码创建一个新数组
- 打印新数组
程序:
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 20)])
print("Original array")
print(arr)
# defining mask
mask = [True, False, True, False, True, True, False, False, False]
# making new array on conditions
new_arr = arr[mask]
print("New array")
print(new_arr)
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 20)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying condition: appending even numbers
if x % 2 == 0:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# defining mask based on two conditions:
# array element must be greater than 15
# and must be a divisible by 2
mask = (arr > 15) & (arr % 2 == 0)
# making new array on conditions
new_arr = arr[mask]
print("New array")
print(new_arr)
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying two conditions: number is divisible by 2 and is greater than 15
if x % 2 == 0 and x > 15:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# using lambda to apply condition
new_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr))
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
输出
Original array
[11 12 13 14 15 16 17 18 19]
New array
[11 13 15 16]
方法二:使用迭代法
开发人员没有使用掩码,而是迭代数组arr并在每个数组元素上应用条件。
方法
- 导入模块
- 创建数组
- 创建一个空数组
- 遍历数组
- 根据某些条件选择项目
- 将所选项目添加到空数组
- 显示阵列
程序:
蟒蛇3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 20)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying condition: appending even numbers
if x % 2 == 0:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
输出
Original array
[11 12 13 14 15 16 17 18 19]
New array
[12 14 16 18]
现在让我们尝试在 NumPy 数组上应用多个条件
方法一:使用遮罩
方法
- 导入模块
- 创建初始数组
- 根据多个条件定义掩码
- 根据掩码向新数组添加值
- 显示阵列
例子
蟒蛇3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# defining mask based on two conditions:
# array element must be greater than 15
# and must be a divisible by 2
mask = (arr > 15) & (arr % 2 == 0)
# making new array on conditions
new_arr = arr[mask]
print("New array")
print(new_arr)
输出
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
方法二:迭代法
方法
- 导入模块
- 创建初始数组
- 创建一个空数组
- 遍历数组
- 根据多个条件选择项目
- 将所选项目添加到空列表
- 显示阵列
例子
蟒蛇3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying two conditions: number is divisible by 2 and is greater than 15
if x % 2 == 0 and x > 15:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
输出
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
方法 3:使用 lambda
方法
- 导入模块
- 创建初始数组
- 使用 lambda函数应用多个条件
- 相应地选择项目
- 将项目添加到新数组
- 显示阵列
例子
蟒蛇3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# using lambda to apply condition
new_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr))
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
输出
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 22 24 26 28 32 34 36 38]