使用 python 屏蔽 mxn 数组

匿名

嗨,我有一个 mxn 数组数据,我想使用 0 和 1 值对其进行屏蔽。如果存在 0 以外的值,我想将其设为 1,而不是 0,我想保持原样。如果我的值是这样的,0.0000609409412即小数点后如果 4 位或更多是零,那么它应该是零而不是 1

Input:
-2.21520694000000e-15   -1.18292704000000e-15   5.42940708000000e-15      
-2.40108895000000e-15    3.09784301000000e-15  -1.18292704000000e-14
 0                       0                      0
 1.50000000000000        2.100000000000000000   1.40000000000000000

output:
1                       1                   1
1                       1                   1
0                       0                   0
1                       1                   1
阿克谢·塞加尔

编辑:根据您的评论更新了我的答案。

试试这个,简单的基于上限和下限的条件,使用 OR|运算符,然后将类型转换为 int。

lower_limit, upper_limit = -0.00001, 0.00001
masked = ((arr<lower_limit)|(arr>upper_limit)).astype('int')

#Testing it out
[str(j)+'-> masked to -> '+str(masked[i]) for i,j in enumerate(arr)]
['0.0-> masked to -> 0',
 '1.0-> masked to -> 1',
 '0.1-> masked to -> 1',
 '0.01-> masked to -> 1',
 '0.001-> masked to -> 1',
 '0.0001-> masked to -> 1',
 '1e-05-> masked to -> 0',
 '1e-06-> masked to -> 0',
 '1e-07-> masked to -> 0',
 '1e-08-> masked to -> 0']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章