python - 了解对 NumPy 数组执行条件索引时出现的 DeprecationWarning 错误(版本 1.11.13,Python 2.7)
问题描述
在对 Numpy 数组执行条件索引时,我不理解弃用警告错误,希望能得到一些澄清,希望它也能造福于社区。让我们考虑一个名为“block”的 NumPy 数组,其中包含从 1 到 12 的整数:
block = np.arange(1,13)
我可以通过执行以下操作来选择不同于 4 的值:
selection = block[block != 4]
现在我想选择不同于 1、4 和 7 的值。如果我这样做:
selection = block[block != np.array([1, 4, 7])]
我收到以下错误:
__main__:1: DeprecationWarning: elementwise != comparison failed; this will
raise an error in the future.
__main__:1: VisibleDeprecationWarning: using a boolean instead of an integer
will result in an error in the future
谁能解释这个警告的原因,并指定执行此切片的正确方法(理想情况下,建议的解决方案也应该适用于尝试从大型 numpy 数组中提取与另一个大型 numpy 数组中的值不同的值时)?请注意,警告后 select = 2 ,我也不明白。
解决方案
你正在做的正确代码是:
selection = block[~np.isin(block, [1, 4, 7])]
推荐阅读
- php - 如何在 php 中使用 date() 显示日期和时间
- node.js - 在 Nodejs 上下文中,为什么“不要阻塞事件循环”这句话即使在开发中也存在,它无法避免?
- java - 随机词生成器,我想限制它从 .txt 文件中读取的词的大小,最多说 3 个字符
- javascript - 缩小谷歌地图故障
- python - 如何在点击它时调用的函数仍在执行时删除 Tkinter 中的按钮?
- html - 仅通过复选框提交选择表单
- android - 如何在 Android Studio 中从 Fragment 打开 Activity
- database - Ionic React 我的组件在使用 map() 首次查看时未呈现
- c++ - Memeber 变量作为 C++ 中的类变量是如何发生的?
- eleventy - 如何让我的 Eleventy 模板不编码 HTML 标签?