English:Feature engineering, scaling and data leakage - AI Year 8
Introduction

Define data fields, fit a transform on training only and compare exact distances. Discover why a changed prediction, an attractive score or more rows does not establish a better model.
Features, units and trustworthy records
Describe a feature before transforming it
A feature is an input representation used for a task. Feature engineering selects or transforms inputs, but a convenient number is not automatically relevant, lawful to use or available when a prediction is needed. Keep an identifier, feature, target, unit, missing-value marker and source record distinct.
Our original example uses fictional display fixtures, not people's heights. Two reference fixtures have height in centimetres, a binary handle flag and an assigned section label. A new query has height 160 and flag zero. Its true section label is not supplied; the exercise compares methods, not measured prediction accuracy.
| Field | Meaning | Role and timing |
|---|---|---|
| record_id | Identifier of an invented fixture | Traceability, not a numeric distance feature |
| height_cm | Positive fixture height in centimetres | Feature available before section selection |
| handle_flag | One if the fixture has the declared handle, otherwise zero | Binary feature, not a second height measurement |
| section_label | Workshop or Gallery in the two reference examples | Supervised reference label, not an input feature |
| allocated_section_after_visit | A later record of the eventual section | Not available at the earlier prediction time |
| source_note | Author-created fictional fixture record | Provenance, not proof that the sample represents a population |
The two references are A: height 150, flag one, label Workshop; and B: height 190, flag zero, label Gallery. Their labels are invented. Two records cannot establish a useful real allocation policy, good coverage or fairness. More copied rows would not repair an unclear field definition or make the invented data representative.
StatQuest with Josh Starmer: K-nearest neighbors, Clearly Explained. Optional background for comparing distances. The verified recording is not the source of our fixtures or a measured accuracy result for this exercise. The complete core calculation is provided below; no real biological data or learner data is needed.
Separate data quality decisions from model choices
Add three proposed records on paper: C with a missing height; a second identical copy of A; and a conflicting A record with the same identifier but label Gallery. Keep the original and document each issue. Hold C for clarification or a separately justified missing-data policy rather than inventing a height. Verify and deduplicate the exact repeated A without counting it as another independent fixture. Stop to resolve the conflicting identity/label rather than silently choosing the more convenient answer.
This procedure does not mean every repeated value is a duplicate or every missing value must be deleted. Two different fixtures may have equal heights. Distinguish repeated identity, legitimate equal values, missingness and conflicting records, then retain a reasoned decision and provenance.
Training-only scaling and exact distances
Fit a scale from training information only
Min-max scaling to a zero-to-one training range subtracts the training minimum m and divides by the training maximum M minus m. Fitting selects those bounds from training data; transforming applies the retained bounds to a value. These are different operations.
Our two training heights give minimum 150 and maximum 190, a range of 40 centimetres. Apply those same bounds to all three example values:
| Height | Subtract training minimum | Divide by training range | Scaled value |
|---|---|---|---|
| 150 | 0 | 40 | 0 |
| 170 | 20 | 40 | 1/2 |
| 190 | 40 | 40 | 1 |
The query height 160 becomes one quarter. A new height 210 becomes three halves, outside zero to one. That is possible when a new value lies outside the original training range; do not silently clip it or refit the bounds to disguise it. Our toy transform does not clip. A constant training feature would give a zero denominator, so this educational function stops and requires a stated policy. Do not assume every library handles constant features in the same way.
Compare raw and scaled squared distances
We compare squared Euclidean distances, adding the squared height difference and the squared flag difference. Taking a square root is unnecessary for ranking nonnegative distances. The two feature meanings differ; raw arithmetic treats their numerical units as given.
For query height 160 and flag zero, A has squared raw distance 101 and B has 900, so raw distance proposes Workshop. Height differences dominate that raw calculation. Now replace each height by its training-fitted scaled value while retaining the binary flag.
| Reference | Raw squared distance | Scaled squared distance |
|---|---|---|
| A: Workshop | 101 | 17/16 |
| B: Gallery | 900 | 9/16 |
Scaled distance proposes Gallery because nine sixteenths is smaller than seventeen sixteenths. The representation changed the relative numerical influence of height and flag. It did not reveal the unknown true query label. We cannot claim scaling improved accuracy merely because it changed the proposal. Its usefulness depends on the method, task and evaluation; it is not a universal improvement for every model.
Inspect the complete exact-arithmetic program
The following original standard-library Python was actually executed. It uses exact integer or Fraction inputs rather than decimal floats, keeps fitting separate from transforming and reports both sets of scores. Its nearest-reference comparison returns ASK if equally near references disagree in label. This is a two-reference teaching example, not a trained deployment-ready classifier or a call to a local generative model.
"""Original fictional display-fixture comparison; no LLM or operational allocation."""
from fractions import Fraction as F
import json
def number(value):
if type(value) not in [int, F]:
raise ValueError("Use exact integers or fractions")
return F(value)
def fit_range(values):
if type(values) not in [list, tuple] or not values:
raise ValueError("Supply nonempty training values")
clean = [number(v) for v in values]
low, high = min(clean), max(clean)
if not high > low:
raise ValueError("This toy requires a stated policy for a constant feature")
return low, high
def transform(value, bounds):
if type(bounds) not in [list, tuple] or len(bounds) != 2:
raise ValueError("Supply the retained training bounds")
low, high = map(number, bounds)
if not high > low:
raise ValueError("The upper training bound must exceed the lower")
return (number(value) - low) / (high - low)
def point(height, flag):
h = number(height)
if not h > 0 or type(flag) is not int or flag not in [0, 1]:
raise ValueError("Use positive fictional fixture height and binary flag")
return h, flag
def compare(query, training, bounds=None):
if type(query) not in [list, tuple] or len(query) != 2:
raise ValueError("Supply height and flag")
qh, qf = point(*query)
if type(training) not in [list, tuple] or not training:
raise ValueError("Supply nonempty labelled reference fixtures")
results = []
for row in training:
if type(row) not in [list, tuple] or len(row) != 3:
raise ValueError("Each reference requires height flag and label")
h, flag = point(row[0], row[1])
label = row[2]
if type(label) is not str or not label:
raise ValueError("Supply a nonempty label")
delta = qh - h if bounds is None else transform(qh, bounds) - transform(h, bounds)
distance = pow(delta, 2) + pow(qf - flag, 2)
results.append(dict(label=label, squared_distance=str(distance)))
best = min(F(r["squared_distance"]) for r in results)
labels = set(r["label"] for r in results if F(r["squared_distance"]) == best)
return dict(scores=results, proposed_label=next(iter(labels)) if len(labels) == 1 else "ASK")
def demonstrate():
training = [(150, 1, "Workshop"), (190, 0, "Gallery")]
bounds = fit_range([row[0] for row in training])
bad_bounds = fit_range([150, 190, 210])
return dict(training_bounds=[str(x) for x in bounds],
three_values=[dict(value=x, transformed=str(transform(x, bounds))) for x in [150, 170, 190]],
outside_training_range=str(transform(210, bounds)),
raw=compare((160, 0), training),
scaled=compare((160, 0), training, bounds),
deliberately_leaky_bounds=[str(x) for x in bad_bounds],
correct_query_height=str(transform(160, bounds)),
deliberately_leaky_query_height=str(transform(160, bad_bounds)),
leaky_comparison=compare((160, 0), training, bad_bounds),
true_query_label_known=False, actual_people_measured=False)
if __name__ == "__main__":
print(json.dumps(demonstrate(), indent=2))If you use an approved Python environment, retain the expected and actual results. If you trace the fractions on paper, label that evidence a paper trace. The function's explicit input contracts are teaching choices, not claims about all data-science libraries.
Leakage, boundaries and a new-domain transfer
Identify two distinct leakage paths
First path: fitting preprocessing on a held-out test value. If the new height 210 is improperly included when fitting, the bounds become 150 and 210. The query height 160 is then one sixth instead of one quarter. Test information has influenced the transformation before evaluation. Split first, fit on training only and apply that retained transform consistently to training and test data.
In our deliberately leaky comparison, the squared distances become thirty-seven thirty-sixths to A and one quarter to B. The proposal happens to remain Gallery. That unchanged answer does not make the fitting procedure valid. We do not invent a score increase or decrease: no true query label was provided.
Second path: including allocated_section_after_visit as an earlier prediction feature. That later outcome is not available when a section must first be proposed. A label-like future field can leak the answer even if the rows were split. Removing that field and separating training/test preprocessing address different problems; neither is replaced by an attractive overall score.
CodeAI: AI, Training Data & Bias. Use the verified recording to discuss data choices and evidence limits. The original fixture numbers and leakage demonstration are not results from the recording. Our tiny sample does not measure a real population, bias level or fairness outcome.
Test boundaries without silently changing the task
Test the training endpoints, the middle value, a value beyond the fitted range and a constant-feature proposal. Retain the same parameters for each transform. Test a missing value and inappropriate input types; the supplied exact-arithmetic functions reject unsupported values rather than inventing measurements.
For an additional distance boundary, use query height 150 with flag zero. Under the scaled comparison, A and B both have squared distance one and their labels disagree, so ASK is the declared tie result. Returning an arbitrary first label would conceal that uncertainty. Record whether a finding concerns a transform, the distance rule, input quality or a missing target label.
The illustration at the start shows generic classifier overfitting, not our fixture data or evidence that scaling cures overfitting. It reminds you why model evaluation must be separate from fitting. An elaborate method and a large dataset still need valid fields, units, provenance and independent tests.
Transfer to a new fictional dataset
For a library task, imagine original book-card page counts and a binary illustrated flag. Rebuild the data dictionary, identify the target and ask when each field is available. Do not reuse 150 and 190 as page-count bounds simply because the old code runs. Fit any transform on the new training data only, retain a missing/duplicate/conflict policy and evaluate the chosen method with appropriate new evidence.
Your report should contain the field dictionary, three correct transformations, raw/scaled scores, both leakage paths, one boundary result, data-quality decisions and a new-context plan. State that the changed fixture proposal has no supplied ground truth, and distinguish actual code execution from paper reasoning and real-world competence.
Interactive Tasks
Quiz: Test Your Knowledge
What does feature engineering do? (Select or transform task-relevant input representations) (!Guarantee that every row is accurate) (!Make every available field a valid input) (!Automatically establish permission for data use)
Which field leaks later outcome information in the earlier prediction task? (The allocated section recorded after the visit) (!The pre-selection fixture height) (!The declared binary handle flag) (!The units in the data dictionary)
Which bounds must fit the height transform here? (Training minimum one hundred fifty and maximum one hundred ninety) (!The maximum of every test value encountered later) (!Any convenient numbers copied from another subject) (!The unknown query label)
What scaled value does height one hundred seventy have? (One half) (!Zero) (!One) (!Three halves)
What scaled value does new height two hundred ten have without clipping? (Three halves) (!One because every test value is forced inside the range) (!Zero because it was not in training) (!An automatically known true label)
Which section does the raw distance calculation propose for the original query? (Workshop) (!Gallery) (!A certified real allocation) (!No section because the rule retrains itself)
Which section does the scaled comparison propose? (Gallery) (!Workshop) (!Both labels are guaranteed correct) (!A new real-world destination)
Why does that changed proposal not prove improved accuracy? (The true query label is not supplied and usefulness is method-dependent) (!Any changed answer must be more accurate) (!Scaling verifies every label) (!The diagram certifies the result)
What is wrong with fitting the range using the held-out height? (Test information influences the transform before evaluation) (!All transformed values become identical) (!The result is automatically unbiased) (!It matters only if the final proposed label changes)
What does the declared scaled tie at height one hundred fifty and flag zero return? (ASK because equally near references disagree) (!The first label without acknowledging the tie) (!A newly generated measurement) (!A guaranteed true section)
Memory Game
| Feature | An input representation used for a task |
| Target | The outcome label a supervised task seeks |
| Fitting | Estimating transform parameters from training data |
| Transforming | Applying retained parameters to values |
| Leakage | Using information unavailable to the intended prediction process |
| Provenance | The origin and permission context of a record |
Drag and Drop
| Keep centimetres and the binary handle meaning explicit | Define fields and units |
| Estimate the bounds using training heights | Fit the transform |
| Apply the retained bounds to a held-out value | Transform consistently |
| Exclude a later outcome from an earlier input | Prevent outcome leakage |
| Resolve same-identity contradictory labels with evidence | Review a data conflict |
Crossword Puzzle
| FEATURE | What input representation is selected or transformed? |
| TARGET | What supervised outcome must stay distinct from inputs? |
| RANGE | What is the training maximum minus minimum? |
| SCALE | What verb describes changing numerical representation? |
| LEAKAGE | What problem uses information outside the permitted fitting or prediction process? |
| SOURCE | What record helps establish a dataset origin? |
LearningApps
Optional extra practice. Choose an appropriate activity with your teacher. If the external collection is unavailable, use the native activities and independent tasks in this course.
Cloze Text
Open Tasks
Easy
- Build the field dictionary: Separate identifier height unit binary flag target later outcome and provenance for the fictional fixtures.
- Transform three heights: Use retained training bounds to calculate zero one half and one for the supplied three values.
- Compare the raw scores: Show the two squared height differences and flag differences for the query, then identify the proposed label.
- Separate fitting and transforming: Explain what each operation reads and why a held-out value must not refit the bounds.
Standard
- Compare scaled distances: Derive both exact fractions and explain the changed proposal without claiming unknown accuracy.
- Test a value outside the range: Transform 210 without clipping and explain why a result above one is possible.
- Trace both leakage paths: Contrast using the held-out height to fit the range with including the later allocated-section field.
- Review data quality additions: Handle the missing-height card, exact duplicate and contradictory same-identity label without inventing data.
Advanced
- Investigate the tie: Derive the equal scaled squared distances at height 150 and flag zero; explain the declared ASK result and a limitation.
- Review a constant feature: Explain the zero denominator and the toy stop policy, then identify what a separately justified alternative would need.
- Challenge a universal scaling claim: Use the method-dependent changed representation and missing true query label to reject an automatic improvement claim.
- Transfer to library cards: Specify page-count and illustrated-flag meanings, new training-only bounds, quality decisions and independent evaluation for a fictional new task.
Learning Check
- Transform without leakage: What are the scaled values 150,170 and 190, and the held-out 210?
- Compare representation effects: Why does the original query change proposed section?
- Identify both leakage paths: What must be fixed in each invalid pipeline?
- Interpret the unchanged leaky proposal: Does the same Gallery output make the leaky fit valid?
- Test uncertainty and data quality: What should a tie and the three proposed bad records teach?
- Transfer the method: What stays and what changes in fictional library cards?
Worked checks and assessment guidance
- With training bounds 150 and 190, the range is 40. The three values transform to 0,1/2 and 1. The held-out 210transforms to 3/2 without clipping; it does not refit the training range. A zero training range requires the declared toy stop and a justified policy rather than division by zero.
- Raw squared distances are 101 to Workshop and 900 to Gallery, so raw chooses Workshop. Scaling only height gives 17/16 and 9/16, so scaled chooses Gallery. The relative numerical influence of height and the binary flag changed. Without a supplied true query label, this is not evidence of improved accuracy or a universally better representation.
- Fitting bounds on training plus held-out 210uses test information; fit on training only and retain the same transform for new data. Including allocated_section_after_visit uses a later outcome unavailable at the earlier prediction time; exclude it from input features. A row split alone does not fix that temporal field leak.
- No. The leaked bounds 150 and 210give query height 1/6 instead of 1/4, and squared distances 37/36 and 1/4. Gallery happens to remain nearer, but test information still affected preparation. No accuracy change can be measured because the query target was not supplied.
- The scaled height150 with flag zero case has squared distances 1 and 1 with different labels, so the declared result is ASK. Hold a missing height for a justified policy; verify an exact duplicate identity before deduplicating; resolve conflicting same-identity labels without silently preferring one. These are different issues, not a reason to invent values.
- Keep clear field roles, units, labels, provenance, missingness and leakage checks. Revisit page-count and illustration meanings, the target, timing, training data and appropriate evaluation. Fit new bounds from the new training data instead of reusing fixture heights. A larger collection or executable code does not by itself establish relevance, permission, representativeness or practical mastery.
Learning evidence
Retain a field dictionary, three exact training-range transformations, the held-out out-of-range value, raw/scaled distance calculations, the unknown-query-target limit, both leakage paths, a tie/constant-feature boundary, data-quality decisions and a fictional library transfer. Preserve exact code and expected/actual outputs when executed. These artefacts do not certify population accuracy, improvement from scaling, legal permission or complete curriculum coverage.
OER and Wikipedia
scikit-learn: Preprocessing data, scaling-features-to-a-range section, and scikit-learn: Common pitfalls, inconsistent preprocessing and data leakage sections read 15 November 2026. The original Fraction program is a separate educational implementation with explicit constant-feature and exact-number contracts, not a claim to reproduce every library behaviour. DfE Computing, Key Stage 3 provides the digital-data context; this Year 8 AI route is editorial enrichment.
Overfitting.svg, Chabacano, CC BY-SA 4.0. Original generic classifier illustration unchanged. It is not our fixture dataset, a scaling experiment or proof that scaling eliminates overfitting.
https://www.youtube.com/watch?v=HVXime0nQeI — StatQuest with Josh Starmer: K-nearest neighbors, Clearly Explained. https://www.youtube.com/watch?v=x2mRoFNm22g — CodeAI: AI, Training Data & Bias. Actual creator, playability, embedding and exact preview bytes verified; original recordings embedded unchanged. No transcript copied, endorsement, redistribution or relicensing implied. The text supplies the complete original core workshop if optional recordings cannot be used.
All fixtures, labels, queries and data-quality additions are fictional and do not measure people. The original standard-library Python was actually executed; proposed query labels are not verified targets. No private learner data, local generative LLM, paid API or operational allocation was used. Preserve expected/actual results and distinguish actual execution from a paper trace.
Linked learning areas
aiMOOC Projects
Schulfach+


aiMOOCs


aiMOOC Projekte


MOOCwiki · Deutsch
Nach dem Lernen ist vor dem Lernen
Entdecke direkt den nächsten Lernkurs. Weitere Inhalte erscheinen, wenn Du weiter nach unten scrollst.
Zur MOOCwiki-HauptseiteMediathek
Mediathek wird aus dem Wiki geladen ...
Keine passenden Inhalte gefunden. Bitte ändere Suche oder Filter.
NEWSLernweltNOAH fragen