Sensors, Annotation and Surveys
The last lesson covered data that already exists in systems. Often it does not exist yet, and you have to collect it: measure the world with sensors, ask people to label examples, or ask people questions. Each collection method leaves its fingerprints on the data, and a model will learn those fingerprints along with the signal unless you know to look for them.
Cameras, microphones and IoT sensors
Physical sensors turn the world into numbers through a chain of hardware and software, and each link can distort the result:
| Effect | What it looks like in data |
|---|---|
| noise | random jitter around the true value |
| calibration offset and drift | a constant or slowly growing error; two devices disagree on the same scene |
| saturation / clipping | values stuck at the sensor's maximum or minimum (an over-exposed white sky, a clipped loud sound) |
| stuck or dead sensor | the same value for hours, or zeros |
| dropouts | gaps when the battery, network or device fails |
| clock errors | timestamps shifted between devices, so events seem to happen in the wrong order |
| device differences | each camera model has its own colours, each microphone its own frequency response |
A few lines of pandas find most of these in a sensor log. This simulated greenhouse sensor has all four classic faults:
import numpy as np
import pandas as pd
rng = np.random.default_rng(3)
t = pd.date_range("2026-07-01", periods=96, freq="15min") # one day
true = 24 + 6 * np.sin(2 * np.pi * (np.arange(96) - 32) / 96)
reading = true + rng.normal(0, 0.3, 96) + np.linspace(0, 1.5, 96) # noise + drift
reading = np.minimum(reading, 30.0) # sensor tops out at 30 °C
reading[80:88] = reading[79] # stuck for 2 hours
s = pd.Series(np.round(reading, 2), index=t).drop(t[20:26]) # 6 readings lost
expected = pd.date_range(s.index[0], s.index[-1], freq="15min")
print("missing timestamps:", len(expected.difference(s.index)),
"from", expected.difference(s.index)[0].strftime("%H:%M"))
print("readings at the 30.0 ceiling:", int((s >= 30.0).sum()))
below = s[s < 30.0] # ignore the clipped values
runs = (below.diff() != 0).cumsum()
longest = below.groupby(runs).size().max()
print("longest run of identical values:", longest, "readings =", longest * 15, "minutes")
drift = (below - true[np.isin(t, below.index)]).resample("6h").mean().round(2)
print("mean error vs reference, per 6 h:", drift.tolist())missing timestamps: 6 from 05:00
readings at the 30.0 ceiling: 17
longest run of identical values: 9 readings = 135 minutes
mean error vs reference, per 6 h: [0.11, 0.59, 0.87, 1.65]
The checks find all four faults: 6 readings missing from 05:00, 17 readings pinned at the 30 °C ceiling during the afternoon, 9 identical readings in a row in the evening (the stuck sensor: its last good value repeated eight times), and an error that grows from 0.11 °C to 1.65 °C over the day. The last line compares the sensor with a reference, which in real life is a calibrated instrument placed next to it for a while. The error grows through the day: drift. Without a reference, drift is very hard to see, which is why calibration schedules exist.
Cameras and microphones raise one more issue: they record people. Faces, voices, licence plates and the inside of homes are personal data, and some places require signs, consent or blurring. See Privacy, Consent, Licensing.
Annotation
When labels do not come for free, people create them. A good annotation project looks more like a small factory than a one-off task:
- Guidelines: a written definition of every class, with examples and counter-examples, and rules for hard cases ("a person partly hidden behind a car still gets a box").
- Pilot: a few annotators label a small batch; disagreements show where the guidelines are unclear. Revise and repeat.
- Gold questions: items with known answers are mixed into the work to measure each annotator's accuracy.
- Overlap: a share of items is labelled by two or more people, to measure agreement (Label Quality) and to resolve conflicts by majority or by an expert.
- Fair working conditions: clear pay, reasonable time per item, and care with disturbing content.
Gold questions in code: each annotator's accuracy on the items whose answer is known.
import pandas as pd
work = pd.DataFrame({
"annotator": ["ann1"] * 6 + ["ann2"] * 6 + ["ann3"] * 6,
"item": ["g1", "g2", "g3", "x1", "x2", "x3"] * 3,
"label": ["cat", "dog", "dog", "cat", "dog", "cat",
"cat", "dog", "dog", "dog", "dog", "cat",
"dog", "cat", "cat", "cat", "cat", "cat"],
})
gold = {"g1": "cat", "g2": "dog", "g3": "dog"}
on_gold = work[work["item"].isin(gold)].copy()
on_gold["correct"] = on_gold["label"] == on_gold["item"].map(gold)
print(on_gold.groupby("annotator")["correct"].mean().rename("gold accuracy"))
print()
votes = work[~work["item"].isin(gold)].pivot(index="item", columns="annotator", values="label")
votes["majority"] = votes.mode(axis=1)[0]
print(votes)annotator
ann1 1.0
ann2 1.0
ann3 0.0
Name: gold accuracy, dtype: float64
annotator ann1 ann2 ann3 majority
item
x1 cat dog cat cat
x2 dog dog cat dog
x3 cat cat cat cat
Annotator 3 got all three gold items wrong, perhaps by swapping the two buttons. Their labels on the real items should be checked or dropped, and the majority recomputed without them. Without the gold items, their votes would look like ordinary disagreement.
Surveys and forms
A survey collects answers that people give about themselves. It is the only way to measure opinions and intentions, and it comes with well-known biases:
| Bias | Example |
|---|---|
| sampling bias | an online survey misses people who are rarely online |
| non-response bias | satisfied customers ignore the survey, angry ones answer |
| wording and order effects | "Do you agree that the new design is better?" pushes towards yes |
| social desirability | people under-report smoking and over-report exercise |
| scale use | some people never pick the extremes of a 1–5 scale |
Answers on agreement scales ("strongly disagree" … "strongly agree", a Likert scale) are ordinal (Numerical, Categorical, Binary and Ordinal Data). Non-response bias is easy to demonstrate: if the chance of answering depends on the answer, the respondents' average is not the population's.
import numpy as np
rng = np.random.default_rng(7)
satisfaction = rng.choice([1, 2, 3, 4, 5], size=10_000, p=[0.05, 0.10, 0.25, 0.35, 0.25])
reply_rate = {1: 0.60, 2: 0.40, 3: 0.15, 4: 0.10, 5: 0.12} # unhappy customers reply more
replied = rng.random(10_000) < np.vectorize(reply_rate.get)(satisfaction)
print("customers:", len(satisfaction), "| replies:", int(replied.sum()))
print("true mean satisfaction: ", round(satisfaction.mean(), 2))
print("mean among respondents: ", round(satisfaction[replied].mean(), 2))
print("share 'unhappy' (1-2), true: ", round((satisfaction <= 2).mean(), 3))
print("share 'unhappy', respondents:", round((satisfaction[replied] <= 2).mean(), 3))customers: 10000 | replies: 1689
true mean satisfaction: 3.65
mean among respondents: 2.97
share 'unhappy' (1-2), true: 0.15
share 'unhappy', respondents: 0.407
The survey makes customers look much less happy than they are: a mean of 2.97 instead of 3.65, and 40.7% "unhappy" instead of 15%. A model trained on such answers inherits the distortion. Remedies include reminders to non-respondents, weighting answers by what is known about the whole population, and comparing respondents with non-respondents on data you do have (age, purchases).
Pitfalls
- Treating sensor output as truth: check gaps, ceilings, stuck values and drift; record device ids and calibration dates as metadata.
- One device, one site: a model trained on one camera or one hospital's scanner may fail on the next. Collect from several, and record which.
- Annotation without guidelines or checks: you get each annotator's private definition of every class.
- Surveys as random samples: who answered is rarely random. Compare respondents with the population.
Practice
- A vibration sensor on a machine reports exactly 0.000 for six hours every night. Is the machine off, or is the sensor broken? What extra data would tell you?
Answer
It could be either. The machine's power or production log (was it running?), a second sensor on the same machine, or the sensor's own status/heartbeat messages would tell them apart. Without them, mark the values as suspicious rather than trusting them.
- In the gold-question example, recompute the majority for
x1without annotator 3.
Answer
Annotators 1 and 2 said cat and dog: a tie. A tie needs a third, reliable vote or an expert decision; it should not be resolved by whichever label happens to come first.
Test yourself
A temperature sensor reports exactly 30.0 for every hot afternoon. This is most likely…
- Drift
- Saturation: the sensor's maximum
- Non-response bias
- A label error
What are gold questions in an annotation project?
- The most expensive items
- Items with known answers mixed into the work to measure annotator accuracy
- Questions written in bold
- The final test set
Unhappy customers answer a satisfaction survey more often than happy ones. The average answer is…
- Accurate
- Biased downwards
- Biased upwards
- Impossible to compute