Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
54 views
in Technique[技术] by (71.8m points)

python - How to find lowest 3 values in a matrix

So i have this fucntion to find the lowest value in a matrix, and return its position in the matrix, ie its indices:

final_matrix=[[3.57 2.71 9.2 5.63]
              [4.42 1.4 3.53 8.97]
              [1.2 0.33 6.26 7.77]
              [6.36 3.6 8.91 7.42]
              [1.59 0.9 2.4 4.24]] # this changes in my code, im just giving a very simple version of it here

def lowest_values(final_matrix):
best_value=10000 #or any arbitrarily high number 
    for i in range(0,len(final_matrix[:,0])):
        for j in range(0,len(final_matrix[0,:])):
            if final_matrix[i,j]<best_value:
                best_value=final_matrix[i,j]
                lowest_val_i=i
                lowest_val_j=j
    return(lowest_val_i, lowest_val_j)

this returns me (1,2), which just by visual analysis is correct. i want to now find the lowest 3 values - hopefully, to build into this loop. But i really cannot think how! Or at least i dont know how to implement it. I was thinking of some if-else loop, that if the lowest value is already found, then 'void' this one and found 2nd lowest, and then same thing to find the third. But im not sure.

Please dont be too quick to shut this question down - im very new to programming, and very stuck!

question from:https://stackoverflow.com/questions/65861702/how-to-find-lowest-3-values-in-a-matrix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The human Approach

I think my approach to this is different enough from different answers to share.

I am only doing 3 comparisons for every list element so it should be O(n). Also I'm not creating a entirely new list with (value, indices) tuple of all the elements.

matrix=[[3.57, 2.71, 9.2, 5.63],
              [4.42, 1.4, 3.53, 8.97],
              [1.2, 0.33, 6.26, 7.77],
              [6.36, 3.6, 8.91, 7.42],
              [1.59, 0.9, 2.4, 4.24]]
              
            
def compare_least_values(value, i, j):
    global least
    if value < least[2][0] :
        if value < least[1][0] :
            if value < least[0][0] :
                least.insert(0, (value,(i,j)))
            else:
                least.insert(1, (value,(i,j)))
        else:
            least.insert(2, (value,(i,j)))
    

def lowest_three_values(matrix): 
    global least
    least = [(10000, (None, None)), (10000, (None, None)), (10000, (None, None))]  
    for i, row in enumerate(matrix):
        for j, value in enumerate(row):
            compare_least_values(value, i, j)
    return least[:3]
    
print(lowest_three_values(matrix))

Output:

[(0.33, (2, 1)), (0.9, (4, 1)), (1.2, (2, 0))]

The practical approach (Numpy)

If you're familiar with numpy than this is the way to go. Even if you're not it can be use as a copy-paste snippet.

import numpy as np

matrix=[[3.57, 2.71, 9.2, 5.63],
              [4.42, 1.4, 3.53, 8.97],
              [1.2, 0.33, 6.26, 7.77],
              [6.36, 3.6, 8.91, 7.42],
              [1.59, 0.9, 2.4, 4.24]]
matrix = np.array(matrix)

indices_1d = np.argpartition(matrix, 3, axis=None)[:3]
indices_2d = np.unravel_index(indices_1d, matrix.shape)
least_three = matrix[indices_2d]

print('least three values : ', least_three)
print('indices : ', *zip(*indices_2d) )

Output:

least three values :  [0.33 0.9  1.2 ] 
indices :  (2, 1) (4, 1) (2, 0)

See this Stackoverflow query for detailed answer on this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...