4 Machine Learning Basics

Learn how machine learning models learn from data, how to choose an evaluation approach, and how to avoid common pitfalls when developing and assessing models.

How models learn from data

builds software that learns patterns from data and uses them to make predictions or decisions. A is the learned function, while an is the learning procedure that adjusts it. Training adjusts the ’s parameters to reduce a , a numerical measure of prediction error.

Data examples contain , or inputs, such as the words in an email. In supervised learning, examples also have a , the desired answer, such as “spam.” The goal is for a to : to perform well on new examples rather than simply memorize its training examples.

Prediction tasks and learning approaches

The kind of prediction determines the task. predicts a numerical value: for example, estimating an apartment’s sale price from its size, location, and age. predicts a category: for example, assigning an email to “spam” or “not spam.”

Learning approaches

  • Supervised learning learns from labeled examples. It is used for and , such as predicting whether a transaction is fraudulent from past transactions labeled as fraudulent or legitimate.

  • Unsupervised learning looks for structure without supplied answer labels. Clustering can group customers with similar behavior, while dimensionality reduction can represent complex data with fewer variables.

  • Semi-supervised learning combines a smaller labeled set with a larger unlabeled set. It is useful when labeling data is costly.

  • Reinforcement learning involves an agent taking actions in an environment and receiving rewards or penalties. The agent aims to improve its choices over time.

Splitting data and preventing

A dataset is commonly divided into separate subsets so that development and evaluation use different examples. Each subset has a distinct role:

  • The training set is used to fit the ’s parameters.

  • The validation set is used during development to compare models or tune choices such as settings and .

  • The test set is held aside until the end to estimate performance on unseen data.

A evaluated on its training examples can appear successful simply because it memorized them. This is : training performance is strong, but performance on new data is weaker. Repeatedly adjusting a based on test results lets information from the test set influence development and makes the final estimate less trustworthy. Keep the test set separate until final evaluation.

The split must suit the data. For example, when predicting future demand, train on earlier dates and test on later dates rather than randomly mixing time periods. Avoid : information that would not genuinely be available at prediction time must not enter training or evaluation. Related or duplicate records should not appear across splits in a way that makes the test artificially easy. No single split ratio suits every dataset; each subset needs enough representative examples for its role.

When data is limited, can help compare models. Divide the development data into several parts, train on all but one part, and validate on the held-out part. Repeat so each part serves as validation. Use these results for selection, then evaluate the chosen once on the untouched test set.

Evaluating performance

Choose evaluation measures that match the task and the cost of different errors. Compare results with a simple baseline, such as always predicting the most frequent class or the average target value.

measures

A counts correct and incorrect predictions. In binary , the counts are true positives, true negatives, false positives, and false negatives.

  • is the fraction of predictions that are correct. It can be misleading when one class is much more common than another.

  • is the fraction of predicted-positive examples that truly are positive. It is important when false alarms are costly.

  • is the fraction of truly positive examples that the finds. It is important when missing positives is costly.

For example, when screening for a rare condition, a that predicts “no condition” for everyone could have high but zero for people who have the condition. alone would hide this failure.

measures

For , is the average absolute difference between predicted and actual values. gives larger errors extra influence. The , denoted by R2R^2, compares a ’s predictions with a simple mean-based baseline; interpret it alongside error measures and the task context.

A single score never captures every kind of quality. Inspect relevant errors and use evaluation data that resembles the data the will encounter.