python - Testing if rows in a numpy array are the same as a given row or different by each element -
this related earlier question: elementwise logical comparison of numpy arrays
i have 2 numpy arrays of random integers
a=np.random.randint(q,size=(n,m)) b=np.random.randint(q,size=(1,m))
i need test if of rows in have more 0 , less m common elements elementwise b.
for example if
a=np.array([[2,0],[0,1],[1,2]]) b=np.array([1,0])
i expect true
since [1,0]
, [1,2]
share more 0 , less 2 elements elemenwise.
on other hand if
b=np.array([2,0])
i expect false
since there rows chare 2 or 0 elements elementwise
at moment approach is:
c=np.where((a[:]==b))[0] n=np.bincount(c) ((n==0)+(n==2)).all()
to me seems convoluted way of testing , wondering if there more natural way i'm missing.
i this
neq=(a==b).sum(-1) result = any(logical_and(neq<b.size, neq>0))
where neq
keeps track of how many digits each line of a
has in common b
.
Comments
Post a Comment