У меня есть две матрицы с одинаковым количеством столбцов, но разным количеством строк, одна намного больше. матA = [[1,0,1],[0,0,0],[1,1,0]], матB = [[0,0,0],[1,0,1],[0,0,0],[1,1,1],[1,1,0]]

Обе они являются пустыми матрицами

Я пытаюсь найти, сколько раз каждая строка matA появляется в matB, и помещаю это в массив, чтобы массив в этом случае стал arr = [1,2,1], потому что первая строка matA появлялась один раз в mat, второй строка появилась два раза, а последняя строка только один раз

0
Latif Fetahaj 15 Апр 2020 в 05:48
Может быть, в B есть элементы, которых нет в A? Тогда мне нужно обработать это в моем ответе.
 – 
Joe
15 Апр 2020 в 08:56

1 ответ

Найти уникальные строки в numpy.array

Каков более быстрый способ получить местоположение уникальных строк в numpy

Вот решение:

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...
0
Joe 15 Апр 2020 в 08:55