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
source
share