检查两个numpy数组python中有多少元素相等 [英] check how many elements are equal in two numpy arrays python
本文介绍了检查两个numpy数组python中有多少元素相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个带有数字(相同长度)的 numpy 数组,我想计算这两个数组之间有多少个元素相等(相等 = 数组中相同的值和位置)
A = [1, 2, 3, 4]B = [1, 2, 4, 3]
然后我希望返回值为 2(只有 1&2 的位置和值相等)
解决方案
使用 numpy.sum
:
I have two numpy arrays with number (Same length), and I want to count how many elements are equal between those two array (equal = same value and position in array)
A = [1, 2, 3, 4]
B = [1, 2, 4, 3]
then I want the return value to be 2 (just 1&2 are equal in position and value)
解决方案
Using numpy.sum
:
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> b = np.array([1, 2, 4, 3])
>>> np.sum(a == b)
2
>>> (a == b).sum()
2
这篇关于检查两个numpy数组python中有多少元素相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文