检查一个numpy数组中有多少个numpy数组等于另一个不同大小的numpy数组中的其他numpy数组 [英] Check how many numpy array within a numpy array are equal to other numpy arrays within another numpy array of different size
问题描述
我的问题
假设我有
a = np.array([ np.array([1,2]), np.array([3,4]), np.array([5,6]), np.array([7,8]), np.array([9,10])])
b = np.array([ np.array([5,6]), np.array([1,2]), np.array([3,192])])
它们是两个大小不同的数组,包含其他数组(内部数组的大小相同!)
They are two arrays, of different sizes, containing other arrays (the inner arrays have same sizes!)
我想计算b中有多少项(即内部数组).请注意,我没有考虑他们的位置!
I want to count how many items of b (i.e. inner arrays) are also in a. Notice that I am not considering their position!
我该怎么做?
我的尝试
count = 0
for bitem in b:
for aitem in a:
if aitem==bitem:
count+=1
有更好的方法吗?尤其是一行,也许有些理解.
Is there a better way? Especially in one line, maybe with some comprehension..
推荐答案
numpy_indexed 软件包包含有效的(通常为nlogn)和矢量化解决方案来解决这些类型的问题:
The numpy_indexed package contains efficient (nlogn, generally) and vectorized solutions to these types of problems:
import numpy_indexed as npi
count = len(npi.intersection(a, b))
请注意,这与您的双循环有一点不同,例如,丢弃a和b中的重复条目.如果您想在b中保留重复项,这将起作用:
Note that this is subtly different than your double loop, discarding duplicate entries in a and b for instance. If you want to retain duplicates in b, this would work:
count = npi.in_(b, a).sum()
也可以通过执行npi.count(a)并考虑其结果来处理a中重复的条目;但是无论如何,我只是出于说明目的而漫步,因为我认为区别可能对您来说并不重要.
Duplicate entries in a could also be handled by doing npi.count(a) and factoring in the result of that; but anyway, im just rambling on for illustration purposes since I imagine the distinction probably does not matter to you.
这篇关于检查一个numpy数组中有多少个numpy数组等于另一个不同大小的numpy数组中的其他numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!