[英]How can I compare two matrices row-wise in python?
I have two matrices with the same number of columns but a different number of rows, one is a lot larger.我有两个列数相同但行数不同的矩阵,其中一个大很多。 matA =
[[1,0,1],[0,0,0],[1,1,0]]
, matB = [[0,0,0],[1,0,1],[0,0,0],[1,1,1],[1,1,0]]
matA =
[[1,0,1],[0,0,0],[1,1,0]]
, matB = [[0,0,0],[1,0,1],[0,0,0],[1,1,1],[1,1,0]]
Both of them are numpy matrices都是numpy矩阵
I am trying to find how many times each row of matA appears in matB and put that in an array so the array in this case will become arr = [1,2,1]
because the first row of matA appeared one time in mat, the second row appeared two times and the last row only one time我试图找出 matA 的每一行出现在 matB 中的次数并将其放入数组中,因此在这种情况下数组将变为 arr =
[1,2,1]
因为 matA 的第一行出现在 mat 中一次,第二行出现两次,最后一行只出现一次
Find unique rows in numpy.array 在 numpy.array 中查找唯一行
What is a faster way to get the location of unique rows in numpy 在 numpy 中获取唯一行位置的更快方法是什么
Here is a solution:这是一个解决方案:
import numpy as np
A = np.array([[1,0,1],[0,0,0],[1,1,0]])
B = np.array([[0,0,0],[1,0,1],[0,0,0],[1,1,1],[1,1,0]])
# stack the rows, A has to be first
combined = np.concatenate((A, B), axis=0) #or np.vstack
unique, unique_indices, unique_counts = np.unique(combined,
return_index=True,
return_counts=True,
axis=0)
print(unique)
print(unique_indices)
print(unique_counts)
# now we need to derive your desired result from the unique
# indices and counts
# we know the number of rows in A
n_rows_in_A = A.shape[0]
# so we know that the indices from 0 to (n_rows_in_A - 1)
# in unique_indices are rows that appear first or only in A
indices_A = np.nonzero(unique_indices < n_rows_in_A)[0] #first
#indices_A1 = np.argwhere(unique_indices < n_rows_in_A)
print(indices_A)
#print(indices_A1)
unique_indices_A = unique_indices[indices_A]
unique_counts_A = unique_counts[indices_A]
print(unique_indices_A)
print(unique_counts_A)
# now we need to subtract one count from the unique_counts
# that's the one occurence in A that we are not interested in.
unique_counts_A -= 1
print(unique_indices_A)
print(unique_counts_A)
# this is nearly the result we want
# now we need to sort it and account for rows that are not
# appearing in A but in B
# will do that later...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.