python - 如何在两个 NumPy 数组之间进行逐元素比较
问题描述
我有两个数组。我想在它们两者之间进行逐元素比较,以找出哪些值相同。
a= np.array([[1,2],[3,4]])
b= np.array([[3,2],[1,4]])
有没有办法让我将这两个数组与 1)找出哪些值相同,以及 2)获取相同值的索引?
加上上一个问题,如果值相同,我有没有办法返回 1,否则返回 0?
提前致谢!
解决方案
a= np.array([[1,2],[3,4]])
b= np.array([[3,2],[1,4]])
#1) find out which values are the same
a==b
# array([[False, True],
# [False, True]])
#2) get the index of the same values?
np.where((a==b) == True) # or np.where(a==b)
#(array([0, 1]), array([1, 1]))
# Adding on to the previous question, is there a way for me to return 1 if the values are the same and 0 otherwise
(a==b).astype(int)
# array([[0, 1],
# [0, 1]])
推荐阅读
- powershell - 添加 AD 组不起作用 [Powershell 命令]
- oracle - 忽略导致错误的行
- postgresql - 无法使用可调用语句或预准备语句调用 PostgreSQL 函数
- android - Android Studio 偶尔无法启动
- c# - 剃刀页面下拉菜单
- logging - log4j 无法更新 wildfly 18 服务器应用程序的日志文件
- react-native - 触发键盘语音输入
- r - ggplot:如何在不覆盖设置比例的情况下使用 scale_fill_discrete 并更改颜色
- python - 使用 Numpy 转置使用 2D 查询窗口迭代 3D 数组
- actor - 为什么 Actor Model 中的 Actor 可以有多个地址