python - 比较两个数组时出错:DeprecationWarning: elementwise comparison failed; 这将在未来引发错误
问题描述
我有一个a
形状为的数组和形状为的(1000000,32)
数组b
(10000,32)
我想找到包含 b 行的 a 的索引。我写了以下代码:
I = np.argwhere((a == b[:, None]).all(axis=2))[:, 1]
当我在其他情况下对其进行测试时,它运行良好。但是对于我当前的数组,它给出了以下错误:
...\Anaconda3\lib\site-packages\ipykernel_launcher.py:111: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
AttributeError: 'bool' object has no attribute 'all'
知道错误的根源是什么吗?谢谢
解决方案
跑:
result = (a[:, np.newaxis] == b).all(-1).any(-1)
脚步:
a[:, np.newaxis] == b
- “按元素”比较。第一个和第二个索引 - 来自a和b的行的索引,第三个索引 - 两行中的列索引。….all(-1)
- a[i]在b[j]中是否有它的“对应物” (两行的所有 元素都相等)。….any(-1)
- a[i]在b的任何行中都有它的“对应物”吗?
要检查每个步骤的结果,请使用 2 个数组,例如最多 10 行和 2 列。
推荐阅读
- snowflake-cloud-data-platform - 问:如何根据小表中的几个值过滤大表而不扫描整个大表?
- regex - 替换括号内的逗号
- java - Java swing菜单控件不对齐
- python - 如何在 argparse 帮助输出中仅抑制长形式的参数?
- powershell - 尝试/捕获或错误变量并使用
- c# - Xamarin WPF - 按钮按下事件不起作用?
- arrays - 是否有 Google 表格功能允许您对具有相似字段但条件不同的不同数据集进行多次查询?
- build - 为什么 ejabberd 想在 /home 中创建 erlang cookie?#crash:权限问题
- html - 如何在 Wordpress 中格式化视频的窗口大小?
- visual-studio-code - 无法远程 SSH 到 VSCode 上的 Windows 主机