Sub topic 5
The Label Matrix: Sources by Examples
Assume you know basic Python, supervised text classification, precision, recall, F1, basic probability, and regular expressions. This chapter shows how weak labeling sources become a matrix that can be inspected and combined.
One row per example, one column per source
A labeling function is a small program that examines text and returns a class or abstains. Abstention means that the source does not have enough evidence to label the example.
The label matrix, usually written as \(L\), stores every result:
- Each row represents one text example.
- Each column represents one labeling function.
- Each cell contains a class label or the abstain value.
-1commonly represents abstention.
For sentiment classification, use 0 for negative and 1 for positive:
| Text | Positive words | Negative words | Thanks |
|---|---|---|---|
| I love this phone | 1 | -1 | -1 |
| Arrived broken | -1 | 0 | -1 |
| Thank you, great service | 1 | -1 | 1 |
| It is a phone | -1 | -1 | -1 |
The matrix preserves the individual sources. Do not replace it with one final label too early. Later steps need to see which sources agreed, disagreed, or stayed silent.
import re
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(text):
pattern = r"\b(great|excellent|love)\b"
return POS if re.search(pattern, text, re.I) else ABSTAIN
def lf_negative_words(text):
pattern = r"\b(refund|broken|terrible)\b"
return NEG if re.search(pattern, 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",
"Arrived broken",
"Thank you, great service",
"It is a phone",
]
L = [[lf(text) for lf in labeling_functions] for text in texts]
print("columns:", [lf.__name__ for lf in labeling_functions])
for text, row in zip(texts, L):
print(row, text)The last row contains only abstentions. That is useful information: the sources do not cover every example.
Inspecting the matrix
The matrix supports diagnostics before any label combination:
- Coverage is the fraction of examples for which a source does not abstain.
- Overlap measures how often two sources label the same example.
- Conflict measures how often two sources label the same example differently.
For example, the positive-word and thanks sources overlap on “Thank you, great service.” If one returned 0 and the other returned 1, that row would contain a conflict.
Why keep the raw matrix?
The raw matrix makes errors traceable. If a final label looks wrong, you can identify the source that caused it. You can also remove, revise, or add a labeling function and recompute the matrix without hand-labeling the entire dataset again.
A later majority vote can produce one label per row. A label model can go further by estimating source reliability and producing probabilities. Both methods depend on this same matrix.
Exercise
What do the values [1, -1, 1] mean for three labeling functions in a binary sentiment task?
Show answer
The first and third sources voted positive, while the second source abstained. No source voted negative.