python-3.x - 使用 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
解决方案
使用 numpy 有许多不同的方法可以实现这一点,例如类型转换:
# given arr is <np.ndarray: m, n>
new_arr = arr.astype(bool).astype(int)
如果要过滤掉低于某个阈值的值,可以执行以下操作:
threshold = 0.0001
new_arr = (arr >= threshold).astype(int)
推荐阅读
- bootstrap-4 - How to add Bootstrap 4 validation design into select2
- java - 从 USats.getForegroundAppPackagename() 返回的字符串值
- typescript - TypeScript:使用异步加载的数据扩展数组中的对象
- typescript - 为什么箭头函数名称必须是 const/let?
- javascript - 如果将复合组件传递给 Waypoint,为什么需要用 React.forwardRef 包装它?
- database - 如何在mysql中存储隐藏值
- mysql - 帖子的多重评分系统(喜欢,爱,明星)
- javascript - 如何将对象中的键与给定的字符串匹配?
- amp-html - 规范 amp 页面上的 amp-ad(adsense 和 GAM)
- django - 如何用用户模型的某些字段更新继承用户模型的模型的字段?