How a model learns.
Connect predictions, adjustable parameters, loss, and the search for a useful rule.
Start with the meaning.
Learning is a process of selecting values for adjustable parts of a model. Consider a rule that predicts a price from area: predicted_price = slope × area + intercept. The slope and intercept are parameters. We do not set them by guessing one house at a time. We supply many example areas and known prices, then choose parameter values that make the model's predictions reasonably close to those known answers. Fitting is the name for this adjustment process.
A prediction is what the current parameter values produce for an input. An error compares that prediction with the known target. A loss function turns errors across training examples into one number the fitting procedure tries to reduce. For squared error, each difference is squared before averaging, so a large mistake counts especially strongly. Other losses express different priorities. The model family and loss together determine what the learner is allowed to call a good fit.
How the idea works.
An algorithm searches the parameter space. Some methods solve a mathematical problem directly; others make repeated small updates. Gradient descent is one update method: it estimates which direction would reduce loss and moves parameters a step in that direction. The learning rate controls the step size. Too large a step can jump over useful values; too small a step can take a long time. This is an optimisation story, not a claim that the computer has discovered a universal law.
A fitted model can only represent rules permitted by its form. A straight line cannot perfectly describe a curve no matter how long we adjust its slope. A very flexible model may fit training examples closely, including accidents and noise, without predicting future cases well. Learning therefore has two questions: can the model represent a useful pattern, and did the fitting procedure select a pattern that generalises? These questions lead to underfitting and overfitting later.
The word “training” refers to fitting parameters from a selected training set. In the common scikit-learn interface, model.fit(X_train, y_train) performs that step and stores learned state in model. model.predict(X_new) later uses the stored state. It does not refit for each new case. Predictions can be numbers, labels, or other structured outputs depending on the task and estimator.
Follow the code.
areas = [50, 70, 90]
prices = [150, 210, 270]
slope = 3
intercept = 0
predictions = [slope * area + intercept for area in areas]
errors = [actual - predicted for actual, predicted in zip(prices, predictions)]
mean_squared_error = sum(error ** 2 for error in errors) / len(errors)
print(predictions, mean_squared_error)This code shows the meaning of a loss without using a library. Each area is multiplied by the slope 3, giving predictions 150, 210, and 270. zip pairs each known price with its corresponding prediction. actual - predicted is zero for every row, so squaring and averaging the errors produces zero. The double star means exponentiation; error ** 2 is error squared. The list comprehensions build new lists rather than changing areas or prices.
A zero training loss here only tells us that this particular rule matches these three examples. If an unseen house has a different price pattern, the rule can still be wrong. It would be especially suspicious if area were only one of many important factors. The useful question is not “can we make this training number small?” but “does the chosen relationship hold for the cases we will actually face?”
What the result does not say.
Some models have hyperparameters, choices set before fitting rather than learned as ordinary parameters. A tree's maximum depth or a nearest-neighbor model's neighbor count is a hyperparameter. Choosing it using the final test set would let evaluation influence the model and spoil the independence of that test. We will give that decision its own validation process.
Loss is a teaching signal, not the whole product goal. A business may care more about some mistakes than others. A model can minimise average loss while systematically failing a small but important group. Evaluation must reconnect the mathematical objective to the real-world cost of errors.
One clear idea.
Fitting chooses model parameters to reduce a loss on examples; useful learning must also generalise.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.