Given multiple prediction vectors, how to efficiently obtain the label with most votes (in numpy/pytorch)?

Viewed 282

I have 3 vectors representing the 3 different predictions of labels for the same data:

P1=[31, 22, 11, 10,  9, 9, 0, 0, 23 ....]  # length over 1M
P2=[31, 22, 12, 10,  8, 9, 0, 0, 30 ....]  # length over 1M
P3=[30, 22, 12, 11,  8, 9, 0, 1, 31 ....]  # length over 1M

Ans= [31, 22, 12, 10, 8, 9, 0, 0, 23, ....]

The basic idea is if a prediction has the highest vote count (e.g. "31" has count 2 in the first column), we pick it, but if all candidates have a different vote (e.g. "23", "30", "31" in the last column), we can pick any one of them.

These vectors may be numpy array, list or pytorch tensor. Considering the length of such vector is over 1000,000, what's the most efficient way (mainly runtime) to find Ans?

2 Answers

Using scipy.mode :

import numpy as np
from scipy.stats import mode

combined = np.array([P1, P2, P3])
majority_vote = mode(combined)[0]

You can take the mode of the tensors:

t = torch.tensor([P1,P2,P3])
t.mode(0).values
tensor([31, 22, 12, 10,  8,  9,  0,  0, 23])
Related