Sub topic 7
Majority Vote as a Baseline
Assume you know basic Python, supervised text classification, precision, recall, F1, regular expressions, and basic probability. This chapter uses those ideas to combine several noisy labeling functions.
A labeling function is a rule that examines text and returns a class label or abstains. For sentiment classification, a function might return POS when it finds “excellent,” NEG when it finds “broken,” or ABSTAIN when it finds no useful clue.
When several labeling functions label the same example, majority vote chooses the class with the most votes. It is a simple baseline. A baseline is a method you use for comparison before trying a more advanced method.
Build a vote from a label matrix
A label matrix stores one row per text and one column per labeling function. The value -1 means abstention.
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
labeling_functions = [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 = [
[function(text) for function in labeling_functions]
for text in texts
]
def majority_vote(row):
votes = [value for value in row if value != ABSTAIN]
if not votes:
return ABSTAIN
ranked = Counter(votes).most_common()
if len(ranked) > 1 and ranked[0][1] == ranked[1][1]:
return ABSTAIN
return ranked[0][0]
print(label_matrix)
print([majority_vote(row) for row in label_matrix])The third text receives both a positive vote from lf_thanks and a negative vote from lf_negative_words. Because the vote is tied, the function abstains. The fourth text receives no votes, so it also abstains.
A tie policy should be explicit. You could choose a fixed class, but that introduces bias. Abstaining is usually safer because uncertain examples can be reviewed or excluded.
Understand what majority vote assumes
Majority vote treats every labeling function as equally reliable. It also treats each vote as independent, although real labeling functions are often related.
| Situation | Majority-vote behavior | Main concern |
|---|---|---|
| Three independent, useful sources agree | Produces a strong simple label | Usually reasonable |
| Two duplicated keyword rules agree | Counts both votes | Correlated evidence is overcounted |
| One accurate rule conflicts with two weak rules | Chooses the weak majority | Source quality is ignored |
| Every source abstains | Returns abstention | Coverage may be low |
Visualize the decision
A majority-vote baseline helps reveal whether your labeling functions contain useful signal. If it performs poorly on the gold set, adding more voting rules without checking their quality may make the dataset worse. A label model can later estimate source accuracies and handle correlated behavior more carefully.
Exercise
Three labeling functions return POS, POS, and NEG for one text. What does majority vote return, and what assumption causes this result?
Show answer
It returns POS. The method assumes each labeling function contributes equally reliable evidence, so the two positive votes defeat the one negative vote.