8 Introduction to Machine Learning
Learn how machine learning uses data, how to train and evaluate models, and how to interpret their performance and limitations.
How uses data
uses data to build models that identify patterns and produce predictions or descriptions. A dataset is often organized into examples, represented by rows, and features, represented by input columns. In , each example also has a label: the value the model is intended to predict. A model is fitted using examples and then used to make predictions about new ones.
Supervised and
learns from examples with known labels and aims to predict labels for new examples. Its two common tasks are , which predicts a category, and , which predicts a numerical value. For example, can predict whether an email is spam, while can predict a home's sale price.
works with data that has no target labels and looks for structure in the features themselves. A clustering algorithm might group customers with similar purchasing patterns. Such groups are patterns found by the algorithm, not automatically meaningful categories, so they require interpretation.
Training and evaluating on separate data
During training, an algorithm uses training data to fit a model, for example by adjusting model parameters to reduce prediction errors. A test set contains separate examples and is used to estimate how well the fitted model performs on data it has not seen. A validation set may also be used during development to compare models or tune choices.
Keep the test set separate until final evaluation. Repeatedly using it to guide decisions can make it less independent as a check. The split should reflect how the model will be used: to predict future demand, for example, testing on later dates may be more appropriate than randomly mixing past and future records.
Check for duplicates and : training features should not contain information that would not actually be available when making a prediction. If a test set is unrepresentative of real-world cases, it can give a misleading estimate.
Generalization and model limitations
A model generalizes when it performs well on new examples. If it performs poorly even on training data, it may be . If it fits training data very closely but performs poorly on unseen data, it may be . Overly complex models and unrepresentative data can contribute to .
A good test score is evidence about performance on data like the test set, not a guarantee of success in every setting. Results can be limited by small or biased samples, noisy or incorrect labels, missing information, and changes in data over time. Even an accurate model may fail when real-world data differs from the training data.
Choosing evaluation measures
Choose evaluation measures that match the prediction task and the consequences of different errors. No single score gives a complete picture.
For , a confusion matrix counts true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN). Common measures are:
Accuracy is the fraction of predictions that are correct:
Accuracy can look high when one class is much more common than another.
is the fraction of predicted positives that are truly positive:
is the fraction of actual positives that the model detects:
F1 score combines and and is high when both are high:
The measure to emphasize depends on the task. In a screening task where missing a true case is costly, may deserve particular attention; when false alarms are costly, may matter more. Accuracy alone may conceal poor performance on a rare class.
For , averages absolute errors, while averages squared errors and therefore penalizes large errors more heavily. Root mean squared error (RMSE) is the square root of MSE and is expressed in the target's units. Lower MAE, MSE, or RMSE indicates smaller errors on the evaluated data.
Unsupervised results are harder to evaluate because there may be no known labels for comparison. A clustering result can be inspected for useful, stable patterns or evaluated against known labels when such labels exist. A numerical score should not replace checking whether the groups are meaningful for their intended purpose.
Interpreting predictions in context
Simple models are useful starting points, but their assumptions and capacity matter. A model that is too simple may miss important patterns, while a more complex model may fit noise and overfit. Evaluation measures summarize selected aspects of performance, not every practical cost or consequence.
Interpret predictions in context and inspect errors. Do not treat a model's output as proof of cause or certainty.