當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Numpy MaskedArray.allequal()用法及代碼示例


在許多情況下,數據集可能不完整或被無效數據汙染。例如,傳感器可能無法記錄數據或記錄了無效值。的numpy.ma模塊通過引入掩碼數組提供了解決此問題的便捷方法。掩碼數組是可能缺少條目或無效條目的數組。

如果a和b的所有條目都相等,則numpy.MaskedArray.allequal()函數將true填充為真值(其中一個或兩個都被屏蔽)時返回True。

用法: numpy.ma.allequal(arr1, arr2, fill_value=True)

參數:
arr1,arr2:[數組]要比較的輸入數組。
fill_value :[bool,可選]是否將arr1或arr2中的掩碼值視為相等(真)或不相等(假)。

Return :[bool]如果兩個數組在給定的公差範圍內相等,則返回True,否則返回False。如果任一數組包含NaN,則返回False。

代碼1:

# Python program explaining 
# numpy.MaskedArray.allequal() method  
  
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
  
# creating 1st input array  
in_arr1 = geek.array([1e8, 1e-5, -15.0]) 
print ("1st Input array:", in_arr1) 
  
# Now we are creating 1st masked array by making third entry as invalid.  
mask_arr1 = ma.masked_array(in_arr1, mask =[0, 0, 1]) 
print ("1st Masked array:", mask_arr1) 
  
# creating 2nd input array  
in_arr2 = geek.array([1e8, 1e-5, 15.0]) 
print ("2nd Input array:", in_arr2) 
  
# Now we are creating 2nd masked array by making third entry as invalid.  
mask_arr2 = ma.masked_array(in_arr2, mask =[0, 0, 1]) 
print ("2nd Masked array:", mask_arr2) 
  
# applying MaskedArray.allequal method 
out_arr = ma.allequal(mask_arr1, mask_arr2, fill_value = False) 
print ("Output array:", out_arr)
輸出:
1st Input array: [ 1.0e+08  1.0e-05 -1.5e+01]
1st Masked array: [100000000.0 1e-05 --]
2nd Input array: [1.0e+08 1.0e-05 1.5e+01]
2nd Masked array: [100000000.0 1e-05 --]
Output array: False

代碼2:

# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
  
# creating 1st input array  
in_arr1 = geek.array([2e8, 3e-5, -45.0]) 
print ("1st Input array:", in_arr1) 
  
# Now we are creating 1st masked array by making third entry as invalid.  
mask_arr1 = ma.masked_array(in_arr1, mask =[0, 0, 1]) 
print ("1st Masked array:", mask_arr1) 
  
# creating 2nd input array  
in_arr2 = geek.array([2e8, 3e-5, 15.0]) 
print ("2nd Input array:", in_arr2) 
  
# Now we are creating 2nd masked array by making third entry as invalid.  
mask_arr2 = ma.masked_array(in_arr2, mask =[0, 0, 1]) 
print ("2nd Masked array:", mask_arr2) 
# applying MaskedArray.allequal method 
out_arr = ma.allequal(mask_arr1, mask_arr2, fill_value = True) 
print ("Output  array:", out_arr)
輸出:
1st Input array: [ 2.0e+08  3.0e-05 -4.5e+01]
1st Masked array: [200000000.0 3e-05 --]
2nd Input array: [2.0e+08 3.0e-05 1.5e+01]
2nd Masked array: [200000000.0 3e-05 --]
Output  array: True


相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy MaskedArray.allequal() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。