Fit, predict and generalize
Fitting estimates model parameters or learned rules from training data. Prediction applies the fitted model to new inputs. Generalization means performing well on examples not used to fit or select the model, under the intended deployment conditions. A baseline is a simple comparator, not a claim of adequate quality. For an inventory classifier, predicting the most frequent training category gives a transparent starting point.
Fit once, then predict
For this fictional dataset, labels are strings: fragile or standard. The training labels are standard, fragile, standard, standard. A constant classifier ignores all input features and learns only the most frequent category. It minimizes the number of training classification errors among constant predictions. In a tie choose the alphabetically first label; empty training data cannot fit this model. The fitted label is passed separately into prediction so that no test labels are used.
def fit_majority(labels): if not labels: raise ValueError('Training labels required') counts = {label: labels.count(label) for label in set(labels)} # For ties, choose the alphabetically first string. return min(counts, key=lambda label: (-counts[label], label))
def predict_constant(label, size): return [label] * size
model = fit_majority(['standard', 'fragile', 'standard', 'standard'])print(model)print(predict_constant(model, 3))print(fit_majority(['standard', 'fragile']))Output: standard, ['standard', 'standard', 'standard'], and fragile. The last result demonstrates deterministic tie-breaking, not a claim that fragile is inherently preferable. Predicting three items repeats the stored label without refitting. The example uses simple list counts for clarity; large datasets need a more efficient implementation.
Compare on held-out examples
Set aside four different fictional items. Their weights are 1, 4, 2 and 6 kg, and their verified categories are fragile, standard, fragile and standard. Use the previously fitted majority model and a fixed rule chosen before looking at these outcomes: predict fragile for weight under 3 kg. Both methods see the same four items. Classification error count is the number of mismatches; divide by four for the error rate. Equal lengths and nonempty evaluation data are required. Comparing this tiny fixture is illustrative, not a statistically reliable test.
def error_count(actual, predicted): if len(actual) != len(predicted) or not actual: raise ValueError('Equal, nonempty evaluation lists required') return sum(a != p for a, p in zip(actual, predicted))
def weight_rule(weights): return ['fragile' if w < 3 else 'standard' for w in weights]
weights = [1, 4, 2, 6]actual = ['fragile', 'standard', 'fragile', 'standard']baseline = ['standard'] * len(weights)rule = weight_rule(weights)print(error_count(actual, baseline))print(error_count(actual, rule))print(error_count(['standard'] * 4, rule))Output: 2, 0, 2. On the first held-out set, the constant classifier has two errors (50%) and the fixed rule has none (0%). In the last line, the rule is applied to a hypothetical second batch whose true categories are all standard, so it makes two errors. The majority classifier would make zero errors on that second batch. Weight is only a hypothetical correlate of fragility; real physical fragility is not determined by a weight threshold. A change in the relationship between features and labels can reverse the apparent winner.
Why a good training score is not enough
An overfit model adapts too closely to training-specific detail and can fail on new examples. An underfit model cannot capture relevant structure even on training data. These are diagnoses that need evidence, not labels you can assign from a single score. A model can score well on common labels while failing on rare ones. Before comparing models define the prediction unit, target, horizon, cost of mistakes and data collection process. Keep evaluation outcomes separate from fitting, and use a separate validation set for model or threshold selection; reserve a final test set for the chosen procedure. Repeatedly choosing the winner on the same test set turns it into selection data. The next chapter explains splits and leakage in detail.
A minimal ML lifecycle
Define the goal and a non-ML alternative; collect authorized representative data and define a target; set aside evaluation data; fit a simple baseline and candidates; evaluate against an agreed metric and operational constraints; deploy only if justified, with monitoring and rollback; review shifts, error cases and new evidence. A model with better accuracy may still cost too much or fail important subgroups. For this toy inventory task no deployment is attempted. Specialist algorithms and model tuning belong in ByteMind Machine Learning; this AI chapter establishes the selection and evaluation vocabulary.
Practice and next step
Fit a majority model using three standard labels and one fragile label. Without changing the fit, predict four new items and compare its error count to the fixed weight rule above. Explain why zero errors on four cases cannot establish production reliability. Next: datasets, splits and experimental hygiene.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Fit, predict and generalize quiz
Apply the learning objectives to new fictional examples.
What is the fitted majority label for [standard, fragile, fragile]?
How does the lesson constant classifier predict a batch of new items?
On the same four held-out items, suppose a baseline makes two errors and another rule makes one. What are their respective error rates?
A frozen model makes no errors on four held-out records. Which conclusion is supported by this result alone?
A team repeatedly tunes thresholds using the final test set. How should it use that set now?