Training and inference
Quick answer
Training, also called fitting, uses available learning data to determine a model’s parameters. Inference uses a fitted model to produce outputs for new inputs. In ordinary fixed-model inference, making a prediction does not update the parameters. Some systems adapt online, but that update is a separate learning operation.
Fit a small model
Use the historical binary labels [1, 0, 1, 1]. This tiny model learns only the most frequent label. The sum of binary labels counts the ones, and len counts all labels. On a tie our teaching model chooses 0.
def fit(labels): ones = sum(labels) zeros = len(labels) - ones return int(ones > zeros)
model = fit([1, 0, 1, 1])print(model)Expected output:
1Three examples are late and one is on time. The learned parameter is 1. The function assumes at least one binary label.
Predict without refitting
The following standalone program fits once and predicts two new parcels. The majority model ignores their features deliberately: it is a simple reference point, not a feature-sensitive predictor.
def fit(labels): return int(sum(labels) > len(labels) - sum(labels))
def predict(model, parcel): return model
model = fit([1, 0, 1, 1])print(predict(model, [2, 10]))print(predict(model, [5, 90]))print(model)Expected output:
111Both parcels receive the same prediction, and the stored model remains 1. The real late outcomes are not required during inference.
Loss and generalization
Loss assigns a numerical penalty to a prediction. For a simple binary classifier, zero-one loss is 0 when the prediction equals the true label and 1 otherwise. The mean of those losses is the fraction of mistakes. Training may minimize a different loss, such as one based on probabilities. Generalization means performing well on new examples from the intended setting.
If the two actual outcomes are 0 and 1, predicting 1 twice produces losses 1 and 0, for a mean of 0.5. New evidence can later be used for a deliberate retraining step.
Common mistake
Do not call fit on a new parcel before predicting it: the true outcome is not yet known. Likewise, do not update your model on evaluation labels while measuring how it performs.
Practice and next steps
Trace the learned parameter for training labels [0, 0, 1], then predict two parcels and calculate zero-one loss if their true labels are 1 and 0. Next, compare the kinds of feedback different learning systems use.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Training and inference — knowledge check
Apply the concepts from this lesson to small fictional scenarios.
What does fitting the majority model on [0, 0, 1] learn?
What does ordinary fixed-model inference do?
A majority model learned 1. What does it predict for two different parcels?
For actual 0 and predicted 1, what is zero-one loss?
What does generalization describe?