Connect Missing Values to Evaluation
A missing value is different from zero. Encoding an unanswered age question as age zero can distort distance calculations. First investigate why values are missing.
Learn replacement values from training data
training = [2, None, 4]
test = [None, 10]
observed = [value for value in training if value is not None]
if not observed:
raise ValueError("No observed training values")
mean = sum(observed) / len(observed)
def fill(values):
return [mean if value is None else value for value in values]
print(fill(training))
print(fill(test))
The outputs are [2, 3.0, 4] and [3.0, 10]. The test value 10 does not influence the mean. If every training value is missing, the example raises an error because no mean can be calculated.
Mean imputation is simple but can alter variance and relationships between features. Consider an extra missingness indicator, collecting more data, or other missing-data methods. Deleting incomplete rows may disproportionately remove a particular group.
Small project: classify fruit
- Problem: Distinguish apples and pears using weight and numeric color features. Define the fruit and conditions covered.
- Data: Record actual types as labels and group repeated observations of the same fruit.
- Split: Divide training, validation, and test data by fruit.
- Prepare: Learn imputation and scaling parameters from training data.
- Compare: Compare a most-common-class baseline with methods such as k-NN.
- Evaluate: Select settings with validation data, then record test results and common failure conditions.
Clustering may help explore the data but is not a mandatory first step. Before choosing a more complex neural network, establish trustworthy data and evaluation.
Check your understanding
Nearly identical photos of the same apple appear in both training and test data. Does a high score establish good performance on new apples?
Show explanation
No. The model may recognize fruit it has already seen. Split by fruit and, where needed, by capture time or environment to evaluate genuinely new observations.