3 Training and Testing Models
Learn how models adjust parameters from examples during training, and how they use those learned parameters to make predictions during inference.
How models learn
A machine-learning model is a mathematical procedure that maps input data to an output. During , the model uses examples to adjust internal values called so that its outputs better match the examples' known answers.
In , each example pairs input features with a target. For instance, the features might describe a home and the target might be its sale price.
The process
A typical process repeats the following steps:
The model receives examples and produces predictions.
A measures how far those predictions are from the examples' known targets.
An optimization method, such as , adjusts the model's to reduce the loss.
The process repeats over individual examples or batches of examples, often for multiple passes through the data. stops according to a chosen rule.
For a simple model that predicts a home's price from its size, the relationship can be written as:
adjusts the weight and bias to make predictions closer to the known prices in the examples. The learned parameter values, together with the model's structure, make up the trained model.
Learning and
Learning is the phase: the model uses examples and a learning procedure to determine or adjust its . happens after , when the model applies those learned to input data to produce an output. does not ordinarily update the model's .
A home-price model can learn from past homes with known sale prices. Later, it can perform on the size of a new home to predict its price. The new home's input is not itself a example unless its known outcome is later used in a process.
Predictions and model use
A is the output a model produces for an input. Its form depends on the task: a model may predict a numerical value, while a model may produce a class, a score, or both. For example, an email classifier might assign a spam score and then use a decision rule to label the email “spam” or “not spam.” A is an estimate, not a guarantee that the result is correct.
In common machine-learning software, fitting a model to data and using it to make predictions are separate operations. In scikit-learn, fit trains an estimator and predict obtains predictions from the fitted estimator. The same fitted model can predict outputs for new inputs without being trained again.