Salta al contenuto
Note per Studenti Logistic Regression Report

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 C1C_1 (positive, ++) and C2C_2 (negative, −-). Let pp be the probability of the positive class; the negative class has probability 1−p1-p.

Odds. The ratio between the probability of the positive and of the negative class:

odds=p1−p.\text{odds}=\frac{p}{1-p}.

The graph (page and notebook) shows odds =0=0 at p=0p=0, odds =1=1 at p=0.5p=0.5, and odds growing without bound as p→1p\to1.

Logit (log-odds): the logarithm of the odds,

logit⁡(p)=log⁡p1−p.\operatorname{logit}(p)=\log\frac{p}{1-p}.

It is a monotonic transformation that maps a value in (0,1)(0,1) onto the whole real line: logit⁡(0.5)=0\operatorname{logit}(0.5)=0, negative for p<0.5p<0.5 (going to −∞-\infty as p→0p\to0), positive for p>0.5p>0.5 (going to +∞+\infty as p→1p\to1).

2. The model and the sigmoid

Sketch of the page: on the x1x_1 axis, objects of class 1 (crosses) accumulate on the right and objects of class 0 (circles) on the left; the curve P(class=1∣x)P(\text{class}=1\mid x) rises from near 00 to near 11, with P(class=0∣x)≈1P(\text{class}=0\mid x)\approx1 far to the left and P(class=1∣x)≈1P(\text{class}=1\mid x)\approx1 far to the right. Let p=P(class=1∣x)p=P(\text{class}=1\mid\mathbf{x}). The assumption of logistic regression is a linear relation with the logit:

logit⁡(P(class=1∣x))=w0x0+w1x1+⋯+wdxd=wTx=z.\operatorname{logit}\big(P(\text{class}=1\mid\mathbf{x})\big)=w_0x_0+w_1x_1+\dots+w_dx_d=\mathbf{w}^T\mathbf{x}=z .

We want pp, not its logit, so invert the relation:

log⁡p1−p=z  ⇒  p1−p=ez  ⇒  p=(1−p)ez  ⇒  p=ez−pez  ⇒  p+pez=ez\log\frac{p}{1-p}=z\;\Rightarrow\;\frac{p}{1-p}=e^{z}\;\Rightarrow\;p=(1-p)e^{z}\;\Rightarrow\;p=e^{z}-pe^{z}\;\Rightarrow\;p+pe^{z}=e^{z}

⇒  p(1+ez)=ez  ⇒  p=ez1+ez=ez/ez1/ez+ez/ez=1e−z+1.\Rightarrow\;p(1+e^{z})=e^{z}\;\Rightarrow\;p=\frac{e^{z}}{1+e^{z}}=\frac{e^{z}/e^{z}}{1/e^{z}+e^{z}/e^{z}}=\frac{1}{e^{-z}+1}.

  p=11+e−z=σ(z)  the (logistic) sigmoid function.\boxed{\;p=\frac{1}{1+e^{-z}}=\sigma(z)\;}\qquad\text{the (logistic) sigmoid function.}

Properties seen in the plot: σ(0)=0.5\sigma(0)=0.5, σ(z)→1\sigma(z)\to1 for z→+∞z\to+\infty, σ(z)→0\sigma(z)\to0 for z→−∞z\to-\infty. The notebook writes it as sigmoid(z) = 1.0 / (1.0 + np.exp(-z)) and plots it on z∈[−7,7]z\in[-7,7].

So the model is a perceptron with the sigmoid at the output: σ(wTx)\sigma(\mathbf{w}^T\mathbf{x}) is the probability that x\mathbf{x} belongs to the positive class. In the notation of the later pages, hθ(x)=θ0x0+θ1x1+⋯=θTxh_\theta(\mathbf{x})=\theta_0x_0+\theta_1x_1+\dots=\theta^T\mathbf{x} is the linear part and hθ(x)=f(θTx)h_\theta(\mathbf{x})=f(\theta^T\mathbf{x}) with ff the activation function (here the sigmoid); y^=hθ(x)\hat y=h_\theta(\mathbf{x}) is the predicted probability.

Decision rule. Following the notebook (and the zero-one loss): assign the object to class y=1y=1 when σ(z)>0.5\sigma(z)>0.5, or equivalently when z>0z>0 (because σ(0)=0.5\sigma(0)=0.5 and σ\sigma is increasing). The decision boundary is again wTx=0\mathbf{w}^T\mathbf{x}=0.

3. The loss function

For one object with label y∈{0,1}y\in\{0,1\} and predicted probability y^\hat y, the likelihood is the Bernoulli probability of the observed label (the notes write "likelihood function for every classifier"):

L(θ)=y^ y(1−y^)1−y,L(\theta)=\hat y^{\,y}(1-\hat y)^{1-y},

since the label is 11 with probability y^\hat y and 00 with probability 1−y^1-\hat y. The log-likelihood is

ℓ(θ)=log⁡L(θ)=ylog⁡y^+(1−y)log⁡(1−y^).\ell(\theta)=\log L(\theta)=y\log\hat y+(1-y)\log(1-\hat y).

Instead of maximising ℓ(θ)\ell(\theta) the course minimises −ℓ(θ)-\ell(\theta), which is a cost function; cost functions are indicated (sometimes) with J(θ)J(\theta):

  J(θ)=−ℓ(θ)=−ylog⁡y^−(1−y)log⁡(1−y^)  \boxed{\;J(\theta)=-\ell(\theta)=-y\log\hat y-(1-y)\log(1-\hat y)\;}

(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 1/m1/m.)

4. Learning: gradient descent

The page gives the gradient and the update, with mm the number of examples and XX 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 →:

∂J(θ)∂θ=1m XT(hθ(X)−y),θnew=θold−η ∂J(θ)∂θ.\frac{\partial J(\theta)}{\partial\theta}=\frac1m\,X^T\big(h_\theta(X)-y\big),\qquad \theta_{\text{new}}=\theta_{\text{old}}-\eta\,\frac{\partial J(\theta)}{\partial\theta}.

Compare with ADALINE: the same form, with the output error hθ(x)−yh_\theta(x)-y 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 y−y^y-\hat y (the gradient step −η 1mXT(y^−y)-\eta\,\frac1m X^T(\hat y-y)).

5. Summary table

model output error minimised stops when
perceptron sign⁡(wTx)\operatorname{sign}(\mathbf{w}^T\mathbf{x}) mistakes (update w+yx\mathbf{w}+y\mathbf{x}) no mistakes
ADALINE wTx\mathbf{w}^T\mathbf{x} squared error stopping rule
logistic regression σ(wTx)\sigma(\mathbf{w}^T\mathbf{x}) J(θ)=−ℓ(θ)J(\theta)=-\ell(\theta) stopping rule

Versione ripasso

Teoria collegata