Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Classification

KTH JupyterHub Open In Colab Binder

In regression, we typically aim to predict a continuous outcome (like a numerical value). However, we can extend the same concepts of regression to classification tasks, where the goal is to predict a discrete class label. By using specific target values (like y=±1y = \pm 1), we can transform regression into a method for classifying data.

The Concept of Classification with Regression

In classification, the target variable yiy_i represents the class label. Common encodings for binary classification are signed labels (yi∈{−1,+1}y_i \in \{-1, +1\}), indicator labels (yi∈{0,1}y_i \in \{0, 1\}), or boolean labels (yi∈{true,false}y_i \in \{\text{true}, \text{false}\}).

In this chapter, we’ll primarily use the signed labels yi∈{−1,+1}y_i \in \{-1, +1\}, but we’ll note where conversions are necessary.

Classification Loss Functions

When applying regression techniques to classification, the choice of loss function is critical. In regression, we typically minimize the sum of squared residuals, but in classification, we use loss functions that are designed to penalize misclassifications proportional to how much the predictor insists on the incorrect prediction. Below are three common loss functions used in classification tasks:

Hinge Loss (Used in Support Vector Machines)

The hinge loss is commonly used in support vector machines (SVMs), which we meet in a later chapter. It assumes signed labels and is defined as:

Lhinge=∑imax⁡(0,1−yif(xi))\mathcal{L}_{\text{hinge}} = \sum_i \max(0, 1 - y_i f(\mathbf{x}_i))

This loss penalizes any data points where the predicted value f(xi)f(\mathbf{x}_i) does not match the true label yiy_i with a sufficient margin. If yif(xi)≥1y_i f(\mathbf{x}_i) \geq 1, the prediction is correct with a margin and there is no penalty. If yif(xi)<1y_i f(\mathbf{x}_i) < 1, there is a penalty proportional to 1−yif(xi)1 - y_i f(\mathbf{x}_i). This means that misclassified points are penalized, but so are correctly classified points that lie too close to the decision boundary (0<yif(xi)<10 < y_i f(\mathbf{x}_i) < 1).

Notebook Cell
Source
<Figure size 1000x600 with 1 Axes>

Logistic Loss (Used in Logistic Regression)

The logistic loss is used in logistic regression and is defined as:

Llogistic=∑ilog⁡(1+exp⁡(−yif(xi)))\mathcal{L}_{\text{logistic}} = \sum_i \log(1 + \exp(-y_i f(\mathbf{x}_i)))

This loss function provides a smooth gradient and penalizes incorrect predictions by increasing the loss for large errors. The form above assumes signed labels yi∈{−1,+1}y_i\in\{-1,+1\} and treats f(xi)f(\mathbf{x}_i) as a real-valued score.

Source
<Figure size 1000x600 with 1 Axes>

Cross-entropy loss

Probably the most used loss function for classification tasks is cross-entropy loss. Here we use indicator labels, yi∈{0,1}y_i \in \{0, 1\}, and instead of a real-valued score the model outputs a predicted probability pi=P(yi=1∣xi)p_i = P(y_i = 1 | \mathbf{x}_i). The loss measures the difference between the predicted probabilities and the actual class labels. It is defined as:

Lcross-entropy=−∑i(yilog⁡(pi)+(1−yi)log⁡(1−pi))\mathcal{L}_{\text{cross-entropy}} = - \sum_{i} \left( y_i \log(p_i) + (1 - y_i) \log(1 - p_i) \right)

For the mathematically inclined reader, this is the negative log-likelihood of a Bernoulli model, so minimizing it corresponds to maximum likelihood estimation for probabilistic predictions. If pip_i is computed from a score f(xi)f(\mathbf{x}_i) with the sigmoid function introduced below, the cross-entropy is exactly the logistic loss above, only written for 0/1 instead of ±1 labels.

Source
<Figure size 1000x600 with 1 Axes>

Classification Example: Logistic Regression

In logistic regression, we model the probability that a data point belongs to class +1 rather than class -1.

The logistic function, also called the sigmoid function, is used to map the output of the linear function f(x)f(\mathbf{x}) to a probability between 0 and 1:

P(yi=+1∣xi)=11+exp⁡(−f(xi))P(y_i = +1 | \mathbf{x}_i) = \frac{1}{1 + \exp(-f(\mathbf{x}_i))}

Where f(xi)=xiTβf(\mathbf{x}_i) = \mathbf{x}_i^T \boldsymbol{\beta} is the linear function whose parameters β\boldsymbol{\beta} we optimize, and xi\mathbf{x}_i includes a constant 1 for the intercept, as in the regression chapter. The logistic loss function is then minimized to find the best-fitting model.

Optimizing the Model for Classification

To solve the classification task, we follow the same procedure used in regression:

  1. Define a loss function: For classification, this could be the hinge loss or logistic loss.

  2. Optimize the loss function: We minimize the loss function with an optimizer, such as gradient descent.

  3. Make predictions: After fitting the model, we predict the class label based on the sign of f(x)f(\mathbf{x}).

Example of Logistic Regression in Code

<Figure size 800x600 with 2 Axes>

The shading shows the predicted probability of class +1. The dashed line is the decision boundary, where f(x)=0f(\mathbf{x}) = 0 and the probability is 0.5; points on either side are assigned the class given by the sign of f(x)f(\mathbf{x}).

What you should know after reading this chapter

You should be able to:

  1. Explain how regression can be used for classification by encoding the classes as signed labels yi∈{−1,+1}y_i \in \{-1, +1\} or indicator labels yi∈{0,1}y_i \in \{0, 1\}, and predicting the class from the sign of f(x)f(\mathbf{x}), or, for a predicted probability, from whether it exceeds 0.5.

  2. Write down the hinge loss, the logistic loss and the cross-entropy loss, state which label encoding each one assumes, and calculate each loss by hand for a single data point.

  3. Interpret plots of the hinge and logistic losses against f(x)f(\mathbf{x}) for y=+1y = +1 and y=−1y = -1. Explain why both losses grow for predictions on the wrong side, and why the hinge loss is exactly zero once yif(xi)≥1y_i f(\mathbf{x}_i) \geq 1.

  4. Explain how logistic regression uses the sigmoid function to turn the linear function f(xi)=xiTβf(\mathbf{x}_i) = \mathbf{x}_i^T \boldsymbol{\beta} into the probability P(yi=+1∣xi)P(y_i = +1 | \mathbf{x}_i), and calculate this probability for a given value of f(xi)f(\mathbf{x}_i).

  5. Interpret a plot of the predicted probability over the feature space, and identify the decision boundary as the points where f(x)=0f(\mathbf{x}) = 0, that is, where the probability is 0.5.