Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

m Smallest values from upper triangular matrix with their indices as a list of tuples

I have a np.ndarray as follows:

[[ inf   1.   3.   2.   1.]
 [ inf  inf   2.   3.   2.]
 [ inf  inf  inf   5.   4.]
 [ inf  inf  inf  inf   1.]
 [ inf  inf  inf  inf  inf]]

Is there a way to get the indices and values of the m smallest items in that nd array? So, if I wanted the 4 smallest it would be

[(0,1,1),(0,4,1),(3,4,1),(0,3,2)] 

where (row,col,val) is the notation above.

If there are multiple values then one of them is just randomly chosen. For instance, there were 3 ones and then next smallest is a value 2 but (0,3,2), (1,2,2),(1,4,2) were all possible choices.

Essentially, Can I extract the k smallest values in that format from the upper triangular matrix efficiently (the matrix is much larger than the example above). I tried flattening it, using square form, nsmallest, but am having trouble getting the indices and values to align. Thanks!

like image 691
Mike El Jackson Avatar asked Feb 01 '17 01:02

Mike El Jackson


People also ask

How do you find the value of the upper triangular matrix?

The determinant of an upper (or lower) triangular matrix is the product of the main diagonal entries. A row operation of type (I) involving multiplication by c multiplies the determinant by c.

How do you find the lower triangular matrix from the upper triangular matrix?

The inverse of unit upper (unit lower) triangular matrix is unit upper (unit lower) triangular. The product of two upper (lower) triangular matrices is upper (lower) triangular matrix. The product of two unit upper (unit lower) triangular matrices is unit upper (unit lower) triangular matrix.

What is upper and lower triangular matrix with example?

Hint: An upper triangular matrix is the one whose elements with i>j are zero while a lower triangular matrix is the one whose elements with i. Complete step-by-step answer: Upper triangular matrix is the one whose elements aij are zero if i>j and may or may not be zero for i≤j . An example of such matrix is: |276035004 ...

What is upper triangular matrix with example?

An upper triangular matrix is a triangular matrix with all elements equal to below the main diagonal. It is a square matrix with element aij where aij = 0 for all j < i. Example of a 2×2matrix. Note: The upper triangular matrices are strictly square matrices.


1 Answers

For an Inf filled array -

r,c = np.unravel_index(a.ravel().argsort()[:4], a.shape)
out = zip(r,c,a[r,c])

For performance, consider using np.argpartition. So, replace a.ravel().argsort()[:4] with np.argpartition(a.ravel(), range(4))[:4].

Sample run -

In [285]: a
Out[285]: 
array([[ inf,   1.,   3.,   2.,   1.],
       [ inf,  inf,   2.,   3.,   2.],
       [ inf,  inf,  inf,   5.,   4.],
       [ inf,  inf,  inf,  inf,   1.],
       [ inf,  inf,  inf,  inf,  inf]])

In [286]: out
Out[286]: [(0, 1, 1.0), (0, 4, 1.0), (3, 4, 1.0), (0, 3, 2.0)]

For a generic case -

R,C = np.triu_indices(a.shape[1],1)
idx = a[R,C].argsort()[:4]
r,c = R[idx], C[idx]
out = zip(r,c,a[r,c])

Sample run -

In [351]: a
Out[351]: 
array([[ 68.,  67.,  81.,  23.,  16.],
       [ 84.,  83.,  20.,  66.,  48.],
       [ 58.,  72.,  98.,  63.,  30.],
       [ 61.,  40.,   1.,  86.,  22.],
       [ 29.,  95.,  38.,  22.,  95.]])
In [352]: out
Out[352]: [(0, 4, 16.0), (1, 2, 20.0), (3, 4, 22.0), (0, 3, 23.0)]

For performance, consider using np.argpartition. So, replace a[R,C].argsort()[:4] with np.argpartition(a[R,C], range(4))[:4].

like image 182
Divakar Avatar answered Oct 24 '22 10:10

Divakar