Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy - count equal arrays

I want to count the number of equal matrices that I encounter after splitting a large matrix.

mat1 = np.zeros((4, 8))

split4x4 = np.split(mat1, 4)

Now I want to know how many equal matrices are in split4x4, but collections.Counter(split4x4) throws an error. Is there a built-in way in numpy to do this?

like image 903
andandandand Avatar asked Oct 30 '22 21:10

andandandand


1 Answers

This can be done in a fully vectorized manner using the numpy_indexed package (disclaimer: I am its author):

import numpy_indexed as npi
unique_rows, row_counts = npi.count(mat1)

This should be substantially faster than using collections.Counter.

like image 81
Eelco Hoogendoorn Avatar answered Nov 02 '22 11:11

Eelco Hoogendoorn