Logistic Regression
In questa pagina 5
A classifier that outputs a probability. It combines the linear model of Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm →, the sigmoid, and the likelihood of Bernoulli Distribution and Maximum Likelihood EstimationA binary outcome (heads = 1, tails = 0) is modelled by a Bernoulli distribution with parameter theta: P(x | theta) = theta^x (1 - theta)^(1 - x). For independent identically distributed observations the likelihood is the product of the single probabilities; with K heads in n tosses it is L(theta) = theta^K (1 - theta)^(n - K). Maximising the log-likelihood l(theta) = K log theta + (n - K) log(1 - theta) (maximum likelihood estimation) gives theta = K / n.Bernoulli Distribution and Maximum Likelihood Estimation →. Code: Python Notebooks - Exercises on Perceptron, Regression and Logistic RegressionThe exercise notebooks of the course: generating two- and three-class data with NumPy distributions, training an ADALINE/perceptron by mini-batch stochastic gradient descent and plotting the errors per epoch, polynomial regression by the closed form w = (X^T X)^{-1} X^T y on a data matrix with columns 1, x, x^2, and logistic regression trained by mini-batch gradient descent on the log loss. Statement and solution code are given, with the inconsistencies between statement and code pointed out.Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression →.
1. Odds and logit
Binary classification problem with classes (positive, ) and (negative, ). Let be the probability of the positive class; the negative class has probability .
Odds. The ratio between the probability of the positive and of the negative class:
The graph (page and notebook) shows odds at , odds at , and odds growing without bound as .
Logit (log-odds): the logarithm of the odds,
It is a monotonic transformation that maps a value in onto the whole real line: , negative for (going to as ), positive for (going to as ).
2. The model and the sigmoid
Sketch of the page: on the axis, objects of class 1 (crosses) accumulate on the right and objects of class 0 (circles) on the left; the curve rises from near to near , with far to the left and far to the right. Let . The assumption of logistic regression is a linear relation with the logit:
We want , not its logit, so invert the relation:
Properties seen in the plot: , for , for . The notebook writes it as sigmoid(z) = 1.0 / (1.0 + np.exp(-z)) and plots it on .
So the model is a perceptron with the sigmoid at the output: is the probability that belongs to the positive class. In the notation of the later pages, is the linear part and with the activation function (here the sigmoid); is the predicted probability.
Decision rule. Following the notebook (and the zero-one loss): assign the object to class when , or equivalently when (because and is increasing). The decision boundary is again .
3. The loss function
For one object with label and predicted probability , the likelihood is the Bernoulli probability of the observed label (the notes write "likelihood function for every classifier"):
since the label is with probability and with probability . The log-likelihood is
Instead of maximising the course minimises , which is a cost function; cost functions are indicated (sometimes) with :
(over a data set the cost is the sum of these terms over the examples; the notebook computes -y.dot(np.log(logreg_output)) - ((1 - y).dot(np.log(1 - logreg_output))), a sum, and the gradient below carries a .)
4. Learning: gradient descent
The page gives the gradient and the update, with the number of examples and the data matrix of ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent →:
Compare with ADALINE: the same form, with the output error computed after the sigmoid instead of after the identity. The notebook trains with mini-batches: w = w + eta * np.dot(np.transpose(X_batch), errors) / X_batch.shape[0] with errors = y_batch - logreg_output, which is the same update written with (the gradient step ).
5. Summary table
| model | output | error minimised | stops when |
|---|---|---|---|
| perceptron | mistakes (update ) | no mistakes | |
| ADALINE | squared error | stopping rule | |
| logistic regression | stopping rule |
Versione ripasso
- = probability of the positive class; odds ; logit maps onto .
- Model: . Inverting:
- with the activation: sigmoid here, identity for ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent →.
- Decision: class 1 when , i.e. ; boundary .
- Sketch: along , class 1 objects accumulate on the right and class 0 on the left; rises from near 0 to near 1 along an S-shaped curve.
- Odds: 0 at , 1 at , unbounded as ; the logit is negative below and positive above.
- Likelihood of one label: ; log-likelihood (see Bernoulli Distribution and Maximum Likelihood EstimationA binary outcome (heads = 1, tails = 0) is modelled by a Bernoulli distribution with parameter theta: P(x | theta) = theta^x (1 - theta)^(1 - x). For independent identically distributed observations the likelihood is the product of the single probabilities; with K heads in n tosses it is L(theta) = theta^K (1 - theta)^(n - K). Maximising the log-likelihood l(theta) = K log theta + (n - K) log(1 - theta) (maximum likelihood estimation) gives theta = K / n.Bernoulli Distribution and Maximum Likelihood Estimation →).
- Cost: (minimise it, not ).
- Gradient ; . Same form as ADALINE, with the error taken after the sigmoid. Notebook: mini-batches.
- Labels coded here; the perceptron uses .
- Typical errors: minimising instead of ; mixing the and codings; computing the logit instead of the probability; forgetting the factor.
- Exercises with the code: Python Notebooks - Exercises on Perceptron, Regression and Logistic RegressionThe exercise notebooks of the course: generating two- and three-class data with NumPy distributions, training an ADALINE/perceptron by mini-batch stochastic gradient descent and plotting the errors per epoch, polynomial regression by the closed form w = (X^T X)^{-1} X^T y on a data matrix with columns 1, x, x^2, and logistic regression trained by mini-batch gradient descent on the log loss. Statement and solution code are given, with the inconsistencies between statement and code pointed out.Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression →.
Teoria collegata
- ADALINE and Gradient Descent
- Bernoulli Distribution and Maximum Likelihood Estimation
- Classification Metrics - Confusion Matrix, Precision, Recall and F1
- Introduction to Machine Learning - Learning, Data and Tasks
- Introduction to Machine Learning - Program Map
- Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression
- Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test