python - 如何为数组中的某些元素获取零值?
问题描述
我的输出数组如下所示。然后我想让所有元素为零,其值介于(0.75 to 1.06)
(0,75 > output > 1.06)
.
array([2. , 1.72787724, 1.45575448, 1.18363171, 0.91150895,
0.63938619, 0.36726343, 0.09514066, 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0.12235294,
0.43529412, 0.74823529, 1.06117647, 1.37411765, 1.68705882,
2. ])
我试过np.where(0.75 >output >1.06,0,1)
了,但它不起作用:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
请帮我解决这样的问题。
提前致谢。
解决方案
您可以使用 numpys 内置的逻辑运算符来执行此操作。尝试:np.logical_and(np.where(output > 0.75), np.where(output < 1.06))
推荐阅读
- python - 如何在 PySpark 中找到数组数组的平均值
- python - 如何修复python中的超出范围错误?
- c++ - 递归似乎只在一定程度上起作用——如何调试这些问题?
- csv - 无法通过 logstash 加载 csv 文件
- r - 重新加载 rStudio 时工作代码停止工作
- javascript - 如何在 ReactJs 中使用 map 渲染字符串数组?
- javascript - 如何为 forwach 循环中的按钮创建事件侦听器
- javascript - Express + React:CSRF Cookie 在生产中未定义,在本地工作
- javascript - Firefox 扩展无法添加请求标头?
- php - WordPress:根据类别数组遍历帖子