How to check if all lines in numpy

Is numpy a good idiomatic way to test if all lines are different in a 2d array?

I thought I could do

len(np.unique(arr)) == len(arr)

but it does not work at all. For instance,

arr = np.array([[1,2,3],[1,2,4]])
np.unique(arr)
Out[4]: array([1, 2, 3, 4])
+4
source share
1 answer

You can calculate the correlation matrix and ask if only the diagonal elements are 1:

(np.corrcoef(M)==1).sum()==M.shape[0]


In [66]:

M = np.random.random((5,8))
In [72]:

(np.corrcoef(M)==1).sum()==M.shape[0]
Out[72]:
True

This is if you want to do the same for columns:

(np.corrcoef(M, rowvar=0)==1).sum()==M.shape[1]

or without numpy:

len(set(map(tuple,M)))==len(M)

Pin unique lines, and then check if the result is the same as Mredundant:

In [99]:

%%timeit

b = np.ascontiguousarray(M).view(np.dtype((np.void, M.dtype.itemsize * M.shape[1])))
_, idx = np.unique(b, return_index=True)

unique_M = M[idx]

unique_M.shape==M.shape
10000 loops, best of 3: 54.6 µs per loop
In [100]:

%timeit len(set(map(tuple,M)))==len(M)
10000 loops, best of 3: 24.9 µs per loop
0
source

All Articles