17
Logistic Regression

Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Logistic Regression

Page 2: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Process

Decide on a model

Find the function which fits the data best Choose a loss function Pick the function which minimizes loss on data

Use function to make prediction on new examples

2

Page 3: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Logistic Regression

Actually classification, not regression :)

Logistic function(or Sigmoid):

Learn P(Y = 1|X = x) using �(wTx), for link function � =<latexit sha1_base64="6YR2mQoAdwRZWy+rKGhBlBEbQ6k=">AAACSnicdVBNTxsxEPWmtIX0K22PXEYklVKpinYDCumhElIvHHoIEoFUSRrNOt5gxWuvbG8h2ub3ceHUGz+CC4cixAU7BAmqdi5+evNm/ObFmeDGhuF5UHqy8vTZ89W18ouXr16/qbx9d2BUrinrUiWU7sVomOCSdS23gvUyzTCNBTuMp199//An04YruW9nGRumOJE84RSto0YVHEjF5ZhJC/CNoZZQG6Roj+K46Mzr3+ELRPALeu49+ViD3HA5cQrDJynWj3/se/YTJEqDMzCFJJfU772XuLHaqFING2G43Wy1wIP21mbkwVYYfm5B5BhfVbKszqjyezBWNE+dJyrQmH4UZnZYoLacCjYvD3LDMqRTnLC+gxJTZobFIoo5fHDMeGEoUe6mBftwosDUmFkaO6U/0/zd8+S/ev3cJu1hwWWWWybp3UdJLsAq8LnCmGtGrZg5gFRz5xXoEWqk1qVfdiHcXwr/BwfNRrTZaO41qzvtZRyrZJ1skDqJyDbZIbukQ7qEklNyQf6Qq+AsuAyug5s7aSlYzrwnj6q0cgstCa7U</latexit>

Features can be discrete or continuous!

P[Y = 1|X = x,w] = �(wTx) =1

1 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Qqa8bBbQFA4MGUAQS7MiDTP6cSIjoVKT0DedeRzqXy+H//N6qQ5OvIxHSapZROcHBakAHUOeNgy4ZFSLiRGESm7uCvSWmEy0+ZOiCeHtpfC5uK7XnKNa/bJRadUXcRTQPvqOqshBx6iFLlAbdRBFz9aKVbI2rRe7YG/Z2/NW21rM7KKlsvdeAW75r0Y=</latexit>

Page 4: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Sigmoid for binary classes

P(Y = 0|w,X) =1

1 + exp(w0 +P

k wkXk)

P(Y = 1|w,X) = 1� P(Y = 0|w,X) =exp(w0 +

Pk wkXk)

1 + exp(w0 +P

k wkXk)

P(Y = 1|w,X)

P(Y = 0|w,X)= exp(w0 +

X

k

wkXk)

Page 5: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Sigmoid for binary classes

P(Y = 0|w,X) =1

1 + exp(w0 +P

k wkXk)

P(Y = 1|w,X) = 1� P(Y = 0|w,X) =exp(w0 +

Pk wkXk)

1 + exp(w0 +P

k wkXk)

P(Y = 1|w,X)

P(Y = 0|w,X)= exp(w0 +

X

k

wkXk)

logP(Y = 1|w,X)

P(Y = 0|w,X)= w0 +

X

k

wkXk

Linear Decision Rule!

Page 6: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Logistic Regression – a Linear classifier

-6 -4 -2 0 2 4 60

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 7: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Process

Decide on a model

Find the function which fits the data best Choose a loss function Pick the function which minimizes loss on data

Use function to make prediction on new examples

7

Page 8: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

P (Y = 1|x,w) = exp(wTx)

1 + exp(wTx)

P (Y = �1|x,w) = 1

1 + exp(wTx)

{(xi, yi)}ni=1 xi 2 Rd, yi 2 {�1, 1}

P (Y = y|x,w) = 1

1 + exp(�y wTx)

■ This is equivalent to:

■ So we can compute the maximum likelihood estimator:

bwMLE = argmaxw

nY

i=1

P (yi|xi, w)

■ Have a bunch of iid data:

Loss function: Conditional Likelihood

Page 9: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

{(xi, yi)}ni=1 xi 2 Rd, yi 2 {�1, 1}

P (Y = y|x,w) = 1

1 + exp(�y wTx)

bwMLE = argmaxw

nY

i=1

P (yi|xi, w)

= argminw

nX

i=1

log(1 + exp(�yi xTi w))

Logistic Loss: `i(w) = log(1 + exp(�yi xTi w))

Squared error Loss: `i(w) = (yi � xTi w)

2

(MLE for Gaussian noise)

■ Have a bunch of iid data:

Loss function: Conditional Likelihood

Page 10: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Process

Decide on a model

Find the function which fits the data best Choose a loss function Pick the function which minimizes loss on data

Use function to make prediction on new examples

10©2018 Kevin Jamieson

Page 11: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Loss function: Conditional Likelihood

{(xi, yi)}ni=1 xi 2 Rd, yi 2 {�1, 1}

P (Y = y|x,w) = 1

1 + exp(�y wTx)

bwMLE = argmaxw

nY

i=1

P (yi|xi, w)

= argminw

nX

i=1

log(1 + exp(�yi xTi w))= J(w)

What does J(w) look like? Is it convex?

■ Have a bunch of iid data:

Page 12: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Loss function: Conditional Likelihood

Page 13: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Loss function: Conditional Likelihood

{(xi, yi)}ni=1 xi 2 Rd, yi 2 {�1, 1}

P (Y = y|x,w) = 1

1 + exp(�y wTx)

bwMLE = argmaxw

nY

i=1

P (yi|xi, w)

= argminw

nX

i=1

log(1 + exp(�yi xTi w))= J(w)

■ Have a bunch of iid data:

Good news: J(w) is convex function of w, no local optima problems

Bad news: no closed-form solution to maximize J(w)

Good news: convex functions easy to optimize

Page 14: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

One other concern… overfitting.

{(xi, yi)}ni=1 xi 2 Rd, yi 2 {�1, 1}

P (Y = y|x,w) = 1

1 + exp(�y wTx)

bwMLE = argmaxw

nY

i=1

P (yi|xi, w)

= argminw

nX

i=1

log(1 + exp(�yi xTi w))

■ Have a bunch of iid data:

Does anyone see a situation when this minimization might overfit?

Page 15: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

= argminw

nX

i=1

log(1 + exp(�yi xTi w)) When is this loss small?

©Kevin Jamieson 2018

Overfitting and Linear Separability

Page 16: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Large parameters → Overfitting

When data is linearly separable, weights ⇒ ∞

Overfitting

Penalize high weights to prevent overfitting?

Page 17: Logistic Regression - University of Washington€¦ · Logistic Regression. Process Decide on a model Find the function which fits the data best Choose a loss function Pick the function

Regularized Conditional Log Likelihood

argminw,b

nX

i=1

log�1 + exp(�yi (x

Ti w + b))

�+ �||w||22

Be sure to not regularize the o↵set b!

Add a penalty to avoid high weights/overfitting?: