📜  Numpy MaskedArray.masked_less_equal()函数| Python

📅  最后修改于: 2022-05-13 01:54:43.391000             🧑  作者: Mango

Numpy MaskedArray.masked_less_equal()函数| Python

在许多情况下,数据集可能不完整或因存在无效数据而受到污染。例如,传感器可能无法记录数据,或记录无效值。 numpy.ma模块通过引入掩码数组提供了一种解决此问题的便捷方法。掩码数组是可能缺少或无效条目的数组。

numpy.MaskedArray.masked_less_equal()函数用于屏蔽小于或等于给定值的数组。此函数是masked_where的快捷方式, condition = (arr <= value).

代码#1:

# Python program explaining
# numpy.MaskedArray.masked_less_equal() method 
  
# importing numpy as geek 
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
  
# creating input array 
in_arr = geek.array([1, 2, 3, -1, 2])
print ("Input array : ", in_arr)
  
# applying MaskedArray.masked_less_equal methods 
# to input array where value<= 2
mask_arr = ma.masked_less_equal(in_arr, 2)
print ("Masked array : ", mask_arr)
输出:
Input array :  [ 1  2  3 -1  2]
Masked array :  [-- -- 3 -- --]

代码#2:

# Python program explaining
# numpy.MaskedArray.masked_less_equal() method 
  
# importing numpy as geek 
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
  
# creating input array 
in_arr = geek.array([5e8, 3e-5, -45.0, 4e4, 5e2])
print ("Input array : ", in_arr)
  
# applying MaskedArray.masked_less_equal methods 
# to input array where value<= 5e2
mask_arr = ma.masked_less_equal(in_arr, 5e2)
print ("Masked array : ", mask_arr)
输出:
Input array :  [ 5.0e+08  3.0e-05 -4.5e+01  4.0e+04  5.0e+02]
Masked array :  [500000000.0 -- -- 40000.0 --]