Linked Questions

2 votes
1 answer
220 views

Is there a numpy limited argsort? [duplicate]

I have an array with 1M floats and want to get the top 10. Wondering if there's an argsort that will only give top 10 to speed things up. I suppose I could do np.argmax() 10 times, dropping the ...
Alexander Soare's user avatar
836 votes
21 answers
810k views

How do I get indices of N maximum values in a NumPy array?

NumPy proposes a way to get the index of the maximum value of an array via np.argmax. I would like a similar thing, but returning the indexes of the N maximum values. For instance, if I have an array, ...
Alexis Métaireau's user avatar
8 votes
2 answers
8k views

N largest values in each row of ndarray

I have an ndarray where each row is a separate histogram. For each row, I wish to find the top N values. I am aware of a solution for the global top N values (A fast way to find the largest N ...
waldol1's user avatar
  • 1,901
3 votes
3 answers
14k views

How to get indices of top-K values from a numpy array

Let suppose I have probabilities from a Pytorch or Keras predictions and result is with the softmax function from scipy.special import softmax probs = softmax(np.random.randn(20,10),1) # 20 instances ...
Deshwal's user avatar
  • 4,292
5 votes
1 answer
8k views

how to get the index of the largest n values in a multi-dimensional numpy array [duplicate]

I want to get the index of the largest n values in a multi-dimensional numpy array. For get the index of the largest n values in a one-dimensional numpy array, i found this. After test in interactive ...
stamaimer's user avatar
  • 6,545
0 votes
4 answers
4k views

More Efficient Way to find the Second Largest Item in a List in Python

I wrote this simple code for a simple task of finding the second largest item in a list of integers: def second_largest(input_list): input_list.sort() return input_list[-2] However, for large ...
Shoshan Ben-Tzvi's user avatar
3 votes
3 answers
2k views

Getting n-th ranked column IDs per row of a dataframe - Python/Pandas

I am trying to find a method for finding the nth ranked value and returning the column name. So for example, given a data-frame: df = pd.DataFrame(np.random.randn(5, 4), columns = list('ABCD')) # ...
ajsp's user avatar
  • 2,690
2 votes
3 answers
1k views

Find indices of x minimum values of a list

I have a list of length n. I want to find the indices that hold the 5 minimum values of this list. I know how to find the index holding the minimum value using operator min_index,min_value = min(...
Mongzyy's user avatar
  • 123
6 votes
1 answer
1k views

Numpy's partition slower than sort for small arrays

I was looking for an efficient way to calculate the nth largest value in a numpy array and this answer lead me to np.partition. By the way, I have noticed that the naive sorting is faster than np....
FLab's user avatar
  • 7,546
0 votes
2 answers
3k views

Python, Numpy, replacing second max value with 1, others with 0

exploring the Internet doesn't give me any results with my problem. I have array like this: y= [[ 2.63321579e-16 9.99986649e-01 2.90973702e-32 9.93230242e-06 1.56965105e-30 1....
JeffTheKiller's user avatar
6 votes
3 answers
937 views

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 ...
Mike El Jackson's user avatar
0 votes
4 answers
705 views

How to identify row and column based on top-K values in Pandas data frame

I have a data frame created this way: import pandas as pd d = {'gene' : ['foo', 'qux', 'bar', 'bin'], 'one' : [1., 2., 3., 1.], 'two' : [4., 3., 2., 1.], 'three' : [1., 2., 20., 1.], ...
neversaint's user avatar
  • 64.5k
1 vote
2 answers
2k views

Time complexity of torch.topk

When m and n of n.topk(m) exceed 20 million and 200,000 respectively, the sorting becomes very slow(over 3 hours). I want to know the time complexity of torch.topk and improvement measures of the ...
zhang xl's user avatar
0 votes
2 answers
1k views

Calculating Euclidean distance with a lot of pairs of points is too slow in Python

The main goal is to generate the customer similarity based on Euclidean distance, and find the 5 most similar customers for each customer. I have 400,000 customers data, each of them has 40 attributes....
ZhaiShang's user avatar
  • 133
0 votes
1 answer
824 views

NumPy: finding N largest elements in a matrix [duplicate]

Edited since my last question was a duplicate, but I'm struggling with this as well. I'm currently working with a matrix and can easily find the largest element with M[M != 1].max() However, I'm ...
planner15's user avatar

15 30 50 per page