How to get 5 tuples with the highest values ​​from the following vector?

I am doing an experiment using gensim. I use the lda model to get a probability vector that looks like this:

[(0, 0.01666666666666804), (1, 0.01666666666666982), (2, 0.01666666666667857), 
 (3, 0.016666666666667104), (4, 0.016666666666668519), (5, 0.01666666666666838), 
 (6, 0.016666666666681464), (7, 0.016666666666669494), (8, 0.016666666666669269), 
 (9, 0.016666666666667069), (10, 0.016666666668398125), (11, 0.016666666666666666), 
 (12, 0.51666666666481131), (13, 0.01666666666668485), (14, 0.01666666666666948), 
 (15, 0.016666666666667097), (16, 0.016666666666666666), (17, 0.016666666666666767), 
 (18, 0.016666666666667922), (19, 0.016666666666678695), (20, 0.016666666666667683), 
 (21, 0.016666666666677307), (22, 0.016666666666669522), (23, 0.016666666666675913), 
 (24, 0.016666666666670923), (25, 0.016666666666667409), (26, 0.016666666666680405), 
 (27, 0.016666666666666666), (28, 0.0166666666666705), (29, 0.016666666666668353)]

This is a list consisting of tuples, the first component of the tuple is the topic, and the second is the probability:

(topic, probability)

I would like to get 5 topics with the highest probability in the list of tuples as follows:

max = [(topicN, probability),...]

I tried first converting this tuple to a numpy structure as follows:

vector = lda[ques_vec]
print(vector)
types = numpy.dtype('int,float')
data = numpy.array(vector,dtype=types)
print(data)

However, I'm not sure how to arrange this structure to get a list of five tuples with the highest probabilities, so I would like to evaluate the support.

+4
source share
1 answer

heapq nlargest:

from heapq import nlargest
from operator import itemgetter

>>> nlargest(5, vector, key=itemgetter(1))  # get the 5 largest values based on the second item in each tuple
[(12, 0.5166666666648113), (10, 0.016666666668398125), (13, 0.01666666666668485), (6, 0.016666666666681464), (26, 0.016666666666680405)]

:

>>> sorted(vec, key=itemgetter(1), reverse=True)[:5]
[(12, 0.5166666666648113), (10, 0.016666666668398125), (13, 0.01666666666668485), (6, 0.016666666666681464), (26, 0.016666666666680405)]

numpy.


numpy, , , :

>>> types = np.dtype('int, float')
>>> data = np.array(vector, dtype=types)
>>> np.sort(data, order='f1')[-5:][::-1]
array([(12, 0.5166666666648113), (10, 0.016666666668398125),
       (13, 0.01666666666668485), (6, 0.016666666666681464),
       (26, 0.016666666666680405)], 
      dtype=[('f0', '<i4'), ('f1', '<f8')])

np.sort (order='f1'), 5 (numpy ) [-5:] , , ([::-1]).

np.sort: (Quicksort). , , , kind='mergesort'.

numpy, : np.partition:

>>> types = np.dtype('int, float')
>>> data = np.array(vector, dtype=types)
>>> np.partition(data, -5, order='f1')[-5:][::-1]
array([(12, 0.5166666666648113), (10, 0.016666666668398125),
       (13, 0.01666666666668485), (6, 0.016666666666681464),
       (26, 0.016666666666680405)], 
      dtype=[('f0', '<i4'), ('f1', '<f8')])

partition , , .

+5

Source: https://habr.com/ru/post/1666865/


All Articles