Sub topic 7
Majority Vote as a Baseline
This chapter assumes you have read the previous chapters on labeling functions and the label matrix. You should be comfortable with Python lists and collections.Counter, and with accuracy and coverage as evaluation measures. The goal is a combiner you can write in ten lines, understand fully, and use as the yardstick for every more complex method later in the tutorial.
What majority vote does
A label matrix has one row per example and one column per labeling function. Each cell holds a class or the abstain value. Majority vote turns a row into one label with three rules:
- Discard every abstention. An abstaining function has no opinion, so it does not count as a vote for anything.
- Count the remaining votes per class and pick the class with the most votes.
- If no function voted, or the top classes are tied, output abstain. The example gets no weak label.
The code below applies these rules to the four review texts from earlier chapters. It also computes coverage, the share of examples on which each function does not abstain.
import re
from collections import Counter
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(text):
return POS if re.search(r'\b(great|excellent|love)\b', text, re.I) else ABSTAIN
def lf_negative_words(text):
return NEG if re.search(r'\b(refund|broken|terrible)\b', text, re.I) else ABSTAIN
def lf_thanks(text):
return POS if 'thank' in text.lower() else ABSTAIN
lfs = [lf_positive_words, lf_negative_words, lf_thanks]
texts = [
'I love this phone, great battery',
'Arrived broken, I want a refund',
'Thank you, but it is terrible',
'It is a phone',
]
# Label matrix: one row per text, one column per labeling function.
L = [[lf(t) for lf in lfs] for t in texts]
def majority_vote(row):
votes = [v for v in row if v != ABSTAIN]
if not votes:
return ABSTAIN
ranked = Counter(votes).most_common()
if len(ranked) > 1 and ranked[0][1] == ranked[1][1]:
return ABSTAIN # tie
return ranked[0][0]
print([majority_vote(row) for row in L]) # [1, 0, -1, -1]
# Coverage: share of examples on which each function does not abstain.
for j, lf in enumerate(lfs):
coverage = sum(row[j] != ABSTAIN for row in L) / len(L)
print(lf.__name__, coverage)The output [1, 0, -1, -1] shows the three possible outcomes:
- Text 1 gets one positive vote and two abstentions, so the label is positive.
- Text 2 gets one negative vote, so the label is negative.
- Text 3 gets one negative vote from
lf_negative_words("terrible") and one positive vote fromlf_thanks. The tie produces an abstention. - Text 4 gets no votes, so it also abstains.
Texts 3 and 4 abstain for different reasons. Text 4 has no evidence. Text 3 has conflicting evidence. Keep that distinction in mind, because the diagnostics from the previous chapter report it as conflict and not as missing coverage.
Tie policies
Abstaining on ties is the safest default, but it is a choice. The table lists the common options.
| Policy | Effect on coverage | Effect on label quality |
|---|---|---|
| Abstain | Lowest | Highest, because contested examples are dropped |
| Random choice | Full | Ties become coin flips, which adds noise |
| Class prior | Full | Biases ties toward the frequent class |
| Defer to a designated function | Full | Only as good as that one function |
Ties are common when you have an even number of voting functions, or when only two functions fire on an example. Check what share of rows end in a tie before choosing a policy. If the share is large, the tie policy matters more than any other detail of your combiner.
Turning votes into soft labels
Hard majority labels throw away information. A row with three positive votes and no others is much more certain than a row with two positive votes and one negative. The simplest soft label is the fraction of non-abstaining votes per class. The next snippet computes it, evaluates the hard label on a small gold set, and reports coverage and accuracy separately.
from collections import Counter
ABSTAIN, NEG, POS = -1, 0, 1
def vote_distribution(row, cardinality=2):
votes = [v for v in row if v != ABSTAIN]
if not votes:
return None
counts = Counter(votes)
return [counts.get(c, 0) / len(votes) for c in range(cardinality)]
def hard_label(row, cardinality=2):
dist = vote_distribution(row, cardinality)
if dist is None:
return ABSTAIN
best = max(dist)
if dist.count(best) > 1:
return ABSTAIN
return dist.index(best)
def evaluate(L, gold):
preds = [hard_label(row) for row in L]
covered = [(p, g) for p, g in zip(preds, gold) if p != ABSTAIN]
coverage = len(covered) / len(gold)
accuracy = sum(p == g for p, g in covered) / len(covered) if covered else float('nan')
return coverage, accuracy
L = [[1, -1, -1], [-1, 0, -1], [-1, 0, 1], [-1, -1, -1], [1, 0, 1]]
gold = [1, 0, 0, 1, 0]
print(vote_distribution(L[4])) # [0.3333333333333333, 0.6666666666666666]
print(evaluate(L, gold)) # (0.6, 0.6666666666666666)The last row has votes [1, 0, 1], so the distribution is one third negative and two thirds positive. The hard label is positive, but the gold label is negative, so that example counts as an error. Two of the three covered examples are right, which gives accuracy 0.67 on 60% coverage.
Always report these two numbers together. Dropping hard examples raises accuracy and lowers coverage, so a single accuracy figure on covered rows can look good while most of the dataset stays unlabeled.
Why it works, and what it assumes
Majority vote is a good baseline for three reasons:
- It has no parameters, so it cannot overfit and needs no training data.
- It is easy to debug. You can read a row and see why the label came out as it did.
- It is often hard to beat when your functions are similarly accurate and mostly independent.
Those last two conditions are the assumptions behind it. The method treats every function as equally reliable and every vote as independent evidence. Both can fail.
Unequal accuracy
A function that is right 95% of the time gets the same single vote as one that is right 55% of the time. If the weak function votes against two strong ones the majority still holds. If two weak functions vote against one strong function, the strong one loses.
Correlated functions
Suppose you write lf_refund and lf_money_back, and both fire on the same complaint texts. They count as two votes but carry nearly one vote of information. The next snippet shows the effect.
from collections import Counter
ABSTAIN, NEG, POS = -1, 0, 1
# Columns: lf_refund, lf_money_back (near duplicates), lf_great.
row = [NEG, NEG, POS]
votes = [v for v in row if v != ABSTAIN]
print(Counter(votes)) # Counter({0: 2, 1: 1})The duplicated negative source wins 2 to 1 even if lf_great is the more trustworthy function for this text. Adding near-duplicate functions quietly shifts the outcome toward their class. This is also how label bias enters. If most of your functions target one class, such as complaints, majority vote will favor it.
Abstention carries signal
Majority vote ignores which functions abstained. A function that fires rarely but is very precise is treated like one that fires often. It also cannot learn that a function tends to vote positive when the true label is negative.
Using it as a baseline
The practical use of majority vote is as a reference point:
- Build the label matrix and run the diagnostics from the previous chapter.
- Compute the majority label for every row and record coverage.
- Score the covered rows against your small gold set, and report precision, recall and F1, not only accuracy.
- Fit a label model on the same matrix and score it the same way.
If the label model does not beat majority vote on the gold set, its extra machinery is not paying for itself. This is common when you have few functions, or when they are nearly equally accurate. If it does win, the gap tells you roughly how much accuracy weighting and correlation handling are worth for your data.
The Snorkel library ships this baseline as MajorityLabelVoter in snorkel.labeling.model. It takes the same label matrix as LabelModel, so swapping one for the other in a comparison needs very little code.
Common errors
- Counting abstain as a class. If
-1is not removed before counting, abstentions win rows where most functions are silent. Filter first. - Reporting accuracy on covered rows only. Without coverage, the number cannot be compared across versions of your functions.
- Ignoring tie rate. An even number of functions often hides a large share of ties that silently become abstentions.
- Using class order as an accidental tiebreak.
maxover a dictionary orargmaxover a list returns the first maximum, which quietly favors the lowest class index.
Exercise
A label matrix row is [POS, POS, NEG, ABSTAIN, NEG, ABSTAIN]. What does a majority vote with the abstain-on-tie policy output, and what would the soft label from vote fractions be? Then say what happens if the two POS functions are near duplicates of each other.
Show answer
The non-abstaining votes are POS, POS, NEG, NEG, so the classes are tied 2 to 2 and the hard label is ABSTAIN. The soft label is [0.5, 0.5] for (NEG, POS). If the two POS functions are near duplicates they carry about one vote of information, so the real evidence leans toward NEG, yet majority vote cannot see this. A label model that estimates correlations or accuracies could break the tie in favor of NEG.
Exercise
Majority vote labels 70% of your training set with 0.82 accuracy on the covered gold examples. A label model labels 100% with 0.80 accuracy on all gold examples. Can you say the label model is worse? What would you check?
Show answer
No. The two numbers are measured on different sets of examples. Majority vote scores only on the 70% it chose to cover, which tend to be the easier ones. To compare fairly, score both methods on the same gold examples, either the covered subset for both or all examples with majority vote's abstentions counted as errors or filled by a fallback. Then compare F1 as well as accuracy.