Machine Learning / Prediction and data foundations / Lesson 2
Data quality and feature contracts
Inspect meaning, alignment, missingness, and measurement policy before fitting a model to apparently tidy arrays.
On this page
Lesson goals
- A row is a measured unit, not just a position
- Measure missingness without confusing it with value
- Separate data inspection from model selection
A row is a measured unit, not just a position
Define the entity and observation represented by each row, the target source, and when each feature becomes available. A table can have correct dimensions while joining the wrong target to every record. Keep stable identifiers and timestamps for auditing and splitting, even if they are not predictive inputs. Distinguish a repeated measurement from an accidental duplicate. Repeated rows from one person, machine, or event are usually statistically related and cannot automatically be treated as independent examples. Record collection and exclusion rules so changes in the dataset do not silently change the population the model is supposed to serve.
Units, ranges, and labels carry assumptions
A numeric column does not reveal whether its values are minutes, seconds, currency, or a coded category. Establish units and plausible ranges, then inspect violations before choosing a repair. A zero can mean a genuine measurement, a missing-value code, or a sensor default; those meanings require different treatment. Labels can be delayed, inconsistently annotated, or influenced by the current operational policy. Document who defines a positive case and whether that definition changes over time. Cleaning should preserve a justified measurement contract rather than deleting inconvenient records until a metric improves. Keep an audit of excluded examples and the reasons for exclusion.
Measure missingness without confusing it with value
The first example counts finite observations by column and identifies rows containing a nonfinite value. It does not yet decide whether to impute, exclude, or repair the record. Missingness may itself reflect a collection process that differs between training and deployment. Learn any imputation statistic from training data only, and preserve a policy for all-missing or newly missing columns. Infinity is also nonfinite but may require a different source investigation from a blank field. A diagnostic count is therefore the beginning of a data-quality decision, not proof that replacing every nonfinite value with zero is valid.
import numpy as np
X = np.array([[1.0, np.nan], [3.0, 4.0], [5.0, 6.0]])
finite = np.isfinite(X)
print(finite.sum(axis=0).tolist())
print((~finite).any(axis=1).tolist())
[3, 2]
[True, False, False]
Column counts and row flags expose missing or nonfinite data without imposing an unreviewed replacement policy.
Join targets by identity rather than accidental order
The second example constructs a target vector in the same identifier order as its feature rows. A separately sorted target list would silently break that pairing. Check identifier uniqueness and expected coverage before the join; many-to-many joins can duplicate observations and alter their effective weights. When repeated observations are legitimate, use a compound observation key or documented aggregation policy rather than discarding them blindly. Preserve the original row identity through filtering and transformations so a later prediction or error can be traced back. A matching row count alone cannot detect a shuffled target or a duplicated entity.
import numpy as np
feature_ids = ["b", "a", "c"]
target_by_id = {"a": 10.0, "b": 20.0, "c": 30.0}
assert len(feature_ids) == len(set(feature_ids))
assert set(feature_ids) <= target_by_id.keys()
y = np.array([target_by_id[key] for key in feature_ids])
print(feature_ids)
print(y.tolist())
['b', 'a', 'c']
[20.0, 10.0, 30.0]
Targets are aligned by explicit identity, not by a separately sorted list. Coverage and uniqueness are checked before construction.
Separate data inspection from model selection
Inspect quality using the agreed training and validation protocol without repeatedly mining the final test outcomes for favorable exclusions. A clear feature schema should state names, order, types, units, allowed categories, missingness, and availability. Validate that schema again at inference, and reject or route incompatible inputs under an explicit fallback policy. Representative summaries matter more than a single overall mean: slices can reveal a unit change or missing-data spike hidden in aggregate. Keep synthetic examples small and interpretable, while recognizing that a real data audit also requires provenance, consent or permitted use, and domain expertise.
Common errors and fixes
Dimensions match but predictions are nonsensical
- Why it happens
- Features and targets were independently reordered.
- How to fix it
- Join by a stable observation identity and verify alignment.
- Prevent it next time
- Preserve ids through every row-changing operation.
Zero imputation creates impossible measurements
- Why it happens
- A generic replacement ignored feature meaning.
- How to fix it
- Choose a training-fitted missingness policy with domain justification.
- Prevent it next time
- Test genuine zero separately from missing input.
Duplicate rows overweight one entity
- Why it happens
- Join multiplicity or repeated data was not audited.
- How to fix it
- Inspect keys and use the intended observation or aggregation grain.
- Prevent it next time
- Report duplicate and entity counts before training.
Check your understanding
1. A sensor column changes from seconds to milliseconds midway through collection. What should happen before modeling?
Show worked solution
Identify the change from provenance and timestamps, convert under an explicit unit policy, and inspect affected slices. Do not ask the model to discover an undocumented scale switch or delete the larger numbers as automatic outliers. Preserve the correction and its evidence in the data contract.
2. Why is a row-count assertion insufficient after joining labels?
Show worked solution
A join can keep the same number while pairing targets incorrectly, or duplicate one entity while dropping another. Verify key coverage, uniqueness or intended multiplicity, and a few independently known associations. Keep the unit of observation stable so evaluation weights and splits retain their meaning.
Key takeaways
- Data meaning is part of the model contract.
- Audit alignment and observation identity.
- Missingness repair requires a justified policy.