Sub topic 2
Measuring coverage, overlap and conflict
This chapter assumes you know basic Python, supervised text classification (features, training, evaluation), precision, recall and F1, and how labeling functions from the earlier chapter work. A labeling function is a Python function that returns a class label or ABSTAIN for one text. Coverage, overlap and conflict describe how those functions vote on your data.
Measuring coverage, overlap and conflict
Run every labeling function on every text to build a label matrix, with one row per text and one column per function. For each function, three numbers matter:
- Coverage: the share of texts where the function votes instead of abstaining.
- Overlap: the share of texts where it votes and at least one other function also votes.
- Conflict: the share of texts where it votes and another function votes with a different label.
The script below computes all three, then runs the rest of the chapter's pipeline.
import numpy as np
from collections import Counter
from scipy.sparse import vstack
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(text):
return POS if any(w in text.lower() for w in ("great", "excellent", "love")) else ABSTAIN
def lf_negative_words(text):
return NEG if any(w in text.lower() for w in ("refund", "broken", "terrible")) else ABSTAIN
def lf_exclamation(text):
return POS if text.endswith("!") else ABSTAIN
lfs = [lf_positive_words, lf_negative_words, lf_exclamation]
texts = [
"I love it!",
"Broken on arrival",
"Great value",
"Terrible, want a refund",
"Excellent service!",
"It works",
"Love the screen",
"Broken hinge, terrible build",
"Great phone, but broken on arrival",
"Thank you, the phone is great",
"Arrived late, no refund yet",
]
L = np.array([[lf(t) for lf in lfs] for t in texts])
voted = L != ABSTAIN
n = len(texts)
# Coverage, overlap and conflict for each labeling function
print("name", "coverage", "overlap", "conflict")
for i, lf in enumerate(lfs):
others = [j for j in range(len(lfs)) if j != i]
coverage = voted[:, i].mean()
overlap = np.mean(voted[:, i] & voted[:, others].any(axis=1))
conflict = np.mean([
voted[r, i] and any(voted[r, j] and L[r, j] != L[r, i] for j in others)
for r in range(n)
])
print(lf.__name__, round(coverage, 2), round(overlap, 2), round(conflict, 2))
# Majority vote as a baseline
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:", [majority_vote(row) for row in L])
# From votes to probabilistic labels (Laplace smoothing)
def soft_label(row, alpha=1.0):
votes = [v for v in row if v != ABSTAIN]
pos = sum(v == POS for v in votes)
return (pos + alpha) / (len(votes) + 2 * alpha)
p_pos = np.array([soft_label(row) for row in L])
print("P(positive):", np.round(p_pos, 2))
# Train a classifier: each text appears once as positive (weight p)
# and once as negative (weight 1 - p).
vec = CountVectorizer()
X = vec.fit_transform(texts)
X_all = vstack([X, X])
y_all = np.array([1] * n + [0] * n)
w_all = np.concatenate([p_pos, 1 - p_pos])
clf = LogisticRegression(max_iter=1000)
clf.fit(X_all, y_all, sample_weight=w_all)
new = vec.transform(["Great screen, I love it", "Arrived broken, want a refund"])
print("P(positive) for new texts:", clf.predict_proba(new)[:, 1].round(2))Majority vote as a baseline
Majority vote takes the most common non-abstaining label for each text. If the top two labels tie, or every function abstains, the text stays unlabeled. This baseline is simple and hard to beat by accident, so compare every later method against it. Its weakness is that it counts every function equally, even when one function is much less accurate than another.
Label models: learning source accuracies without ground truth
A label model estimates how accurate each labeling function is, using only the pattern of agreements and disagreements in the label matrix. Functions that agree with most other functions are probably accurate. Frameworks such as Snorkel implement this idea with a probabilistic model.
> Label models assume that labeling functions make errors independently of each other. Correlated functions, such as two keyword lists that share most words, break this assumption and make the model overconfident.
From votes to probabilistic labels
Majority vote gives one hard label per text. A probabilistic label gives a probability for each class instead. The soft_label function above uses the share of positive votes, smoothed so that a text with no votes gets 0.5 rather than a confident guess. Soft labels keep uncertainty that hard labels discard.
Training a classifier on weak labels
Any classifier that accepts sample weights can train on soft labels. The script writes each text twice, once as positive with weight p and once as negative with weight 1 - p. The classifier then learns from the soft labels without any hard labels. Evaluate the result on a small hand-labeled gold set, because weak labels can look reasonable while being systematically wrong.
Exercise
A text receives no votes from any labeling function. What soft label does soft_label return for it, and what effect does that have on training?
Show answer
It returns 0.5. The positive copy and the negative copy of that text get equal weight, so they cancel out and the text adds no preference toward either class.