Machine learning starts with the input and target
Work through file-transfer data to identify features, targets, missing values and units before fitting a model.
Start supervised learning with the data the model can actually see. We will use completed file transfers. Size in MB is the input. Measured duration in seconds is the target. For a transfer that has not happened yet, only the size is available.
All examples below use sample data. Run each independently with Python 3. The printed outputs were checked locally.
Features and targets
Each completed transfer is one example. File size is known before a transfer starts. Elapsed time is the value to predict and is measured afterwards.
Two rows pair sizes 1 and 2 MB with measured times 2 and 4 seconds. A 3 MB file has an input but no observed target yet.
rows = [(1,2),(2,4)]
print(rows)
print("new input", 3, "target", None)
Expected output
[(1, 2), (2, 4)]
new input 3 target None
Reference: https://developers.google.com/machine-learning/glossary#feature
Extract a feature column
A feature column contains one input value per example. Extract file size without passing the measured duration into the prediction input.
Two examples give two feature values, [1, 2]. The feature vector has length 2.
rows = [(1,2),(2,4)]
features = [size for size,seconds in rows]
print(features, len(features))
Expected output
[1, 2] 2
Reference: https://developers.google.com/machine-learning/glossary#feature
Keep target values aligned with their inputs
A target belongs to the same example as its input. Separate the columns while preserving row order, then zip them back together to check alignment.
The pairs must remain (1, 2) and (2, 4). Swapping targets changes the training examples.
x, y = [1,2], [2,4]
print(list(zip(x,y)))
print(list(zip(x,reversed(y))))
Expected output
[(1, 2), (2, 4)]
[(1, 4), (2, 2)]
Reference: https://developers.google.com/machine-learning/glossary#feature
Exclude data recorded after prediction time
A feature must be available when the prediction is made. Transfer duration is the target. Putting the measured duration into the feature set leaks the answer.
If a prediction simply copies its target, absolute training error is |y - y| = 0. That input is unavailable before a new transfer finishes.
targets = [2,4]
leaked_predictions = targets[:]
print([abs(p-y) for p,y in zip(leaked_predictions,targets)])
Expected output
[0, 0]
Reference: https://developers.google.com/machine-learning/glossary#feature
Distinguish a missing target from zero
None means this transfer has no measured duration yet. Zero is a numerical observation. Keep them distinct so missing observations do not become zero-duration training examples.
For [2, 4, missing], there are two observed targets. Filling missing with zero changes the mean from 3 to 2.
values = [2,4,None]
known = [v for v in values if v is not None]
print(sum(known)/len(known))
print(sum(known)/len(values))
Expected output
3.0
2.0
Reference: https://developers.google.com/machine-learning/glossary#feature
Keep the units in the model definition
Changing MB to kB changes the numerical input scale. Under the decimal convention, 1 MB equals 1,000 kB. A fitted weight must use the matching unit.
A weight of 2 seconds per MB is 0.002 seconds per kB. Both predict 6 seconds for a 3 MB file.
mb = 3
kb = mb * 1000
print(mb * 2, kb * .002)
Expected output
6 6.0
Reference: https://developers.google.com/machine-learning/glossary#feature
Validate the next prediction input
Check the shape and domain before using new input. This example expects one strictly positive numeric file size. A missing value and a negative size should be rejected.
The valid input set is x > 0 with x numeric. Of [3, None, -1], one input is valid.
def valid(x):
return type(x) in (int,float) and x > 0
print([valid(x) for x in [3,None,-1]])
Expected output
[True, False, False]
Reference: https://developers.google.com/machine-learning/glossary#feature
Before fitting a model, verify the meaning, availability, units and alignment of each input and target. Keep examples with missing targets separate from completed observations. The next lesson can then compare a prediction with an observed answer.
The team guide · My original notes on testing coding agents · Working with Lightbulb Partners