Transcript
Page 1: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic RegressionThomas Schwarz

Page 2: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Categorical Data• Outcomes can be categorical

• Often, outcome is binary:

• President gets re-elected or not

• Customer is satisfied or not

• Often, explanatory variables are categorical as well

• Person comes from an under-performing school

• Order was made on a week-end

• …

Page 3: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Famous example:

• Taken an image of a pet, predict whether this is a cat or a dog

Page 4: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Bayes: generative classifier

• Predicts indirectly

• Evaluates product of likelihood and prior

• Prior: Probability of a category without looking at data

• Likelihood: Probability of observing data if from a category

P(c |d)

c =  arg maxc∈CP(d |c)P(c)

c

c

PriorLikelihood

Page 5: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Regression is a discriminative classifier

• Tries to learn directly the classification from data

• E.g.: All dog pictures have a collar

• Collar present —> predict dog

• Collar not present —> predict cat

• Computes directly P(c |d)

Page 6: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Regression:

• Supervised learning: Have a training set with classification provided

• Input is given as vectors of numerical features

• Classification function that calculates the predicted class

• An objective function for learning: Measures the goodness of fit between true outcome and predicted outcome

• An algorithm to optimize the objective function

x(i) = (x1,i, x2,i, …, xn,i)

y(x)

Page 7: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Linear Regression:

• Classification function of type

• Objective function (a.k.a cost function)

• Sum of squared differences between predicted and observed outcomes

• E.g. Test Set

• Minimize cost function

y ((x1, x2, …, xn)) = a1x1 + a2x2 + …anxn + b

T = {x(1), x(2), …x(m)}m

∑i=1

(y(i) − y(i))2

Page 8: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Linear regression can predict a numerical value

• It can be made to predict a binary value

• If the predictor is higher than a cut-off value: predict yes

• Else predict no

• But there are better ways to generate a binary classifier

Page 9: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Prediction Models for Binary Outcomes

• Good binary classifier:

• Since we want to predict the probability of a category based on the features:

• Should look like a probability

• Since we want to optimize:

• Should be easy to differentiate

• Best candidate classifier that has emerged:

• Sigmoid classifier

Page 10: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Use logistic function

σ(z) =1

1 + exp(−z)

-10 -5 5 10

0.2

0.4

0.6

0.8

1.0

��'

Page 11: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Combine with linear regression to obtain logistic

regression approach:

• Learn best weights in

• We know interpret this as a probability for the positive outcome '+'

• Set a decision boundary at 0.5

• This is no restriction since we can adjust and the weights

y ((x1, x2, …, xn)) = σ(b + w1x1 + w2x2 + …wnxn)

b

Page 12: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• We need to measure how far a prediction is from the true

value

• Our predictions and the true value can only be 0 or 1

• If : Want to support and penalize .

• If : Want to support and penalize .

• One successful approach:

y y

y = 1 y = 1 y = 0

y = 0 y = 0 y = 1

Loss( y, y) = yy(1 − y)(1−y)

Page 13: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Easier: Take the negative logarithm of the loss function

• Cross Entropy Loss

LCE = − y log( y) − (1 − y)log(1 − y)

Page 14: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• This approach is successful, because we can use Gradient Descent

• Training set of size

• Minimize

• Turns out to be a convex function, so minimization is simple! (As far as those things go)

• Recall:

• We minimize with respect to the weights and

mm

∑i=1

LCE(y(i), y(i))

y ((x1, x2, …, xn)) = σ(b + w1x1 + w2x2 + …wnxn)b

Page 15: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Calculus:

• Difference between true and estimated outcome , multiplied by input coordinate

δLCE(w, b)δwj

= (σ(w1x1 + …wnxn + b) − y) xj

= ( y − y)xj

y y

Page 16: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Stochastic Gradient Descent

• Until gradient is almost zero:

• For each training point :

• Compute prediction

• Compute loss

• Compute gradient

• Nudge weights in the opposite direction using a learning weight

• Adjust

x(i), y(i)

y(i)

η

(w1, …, wn) ← (w1, …, wn) − η∇LCE

η

Page 17: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression• Stochastic gradient descent uses a single data point

• Better results with random batches of points at the same time

Page 18: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Lasso and Ridge Regression

• If the feature vector is long, danger of overfitting is high

• We learn the details of the training set

• Want to limit the number of features with positive weight

• Dealt with by adding a regularization term to the cost function

• Regularization term depends on the weights

• Penalizes large weights

Page 19: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Lasso and Ridge Regression

• L2 regularization:

• Use a quadratic function of the weights

• Such as the euclidean norm of the weights

• Called Ridge Regression

• Easier to optimize

Page 20: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Lasso and Ridge Regression

• L1 regularization

• Regularization term is the sum of the absolute values of weights

• Not differentiable, so optimization is more difficult

• BUT: effective at lowering the number of non-zero weights

• Feature selection:

• Restrict the number of features in a model

• Usually gives better predictions

Page 21: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Examples• Example: quality.csv

• Try to predict whether patient labeled care they received as poor or good

Page 22: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Examples• First column is an arbitrary patient ID

• we make this the index

• One column is a Boolean, when imported into Python

• so we change it to a numeric value

df = pd.read_csv('quality.csv', sep=',', index_col=0) df.replace({False:0, True:1}, inplace=True)

Page 23: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Examples• Farmington Heart Data Project:

• https://framinghamheartstudy.org

• Monitoring health data since 1948

• 2002 enrolled grandchildren of first study

Page 24: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Examples

Page 25: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Examples• Contains a few NaN data

• We just drop them

df = pd.read_csv('framingham.csv', sep=',') df.dropna(inplace=True)

Page 26: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Import statsmodels.api

• Interactively select the columns that gives us high p-values

import statsmodels.api as sm

cols = [ 'Pain', 'TotalVisits', 'ProviderCount', 'MedicalClaims', 'ClaimLines', 'StartedOnCombination', 'AcuteDrugGapSmall',]

Page 27: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Create a logit model

• Can do as we did for linear regression with a string

• Can do using a dataframe syntax

• Print the summary pages

logit_model=sm.Logit(df.PoorCare,df[cols]) result=logit_model.fit()

print(result.summary2())

Page 28: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Print the results

• This gives the "confusion matrix"

• Coefficient [i,j] gives:

• predicted i values

• actual j values

print(result.pred_table())

Page 29: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Quality prediction:

• 7 False negative and 18 false positives

[[91. 7.] [18. 15.]]

Page 30: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Heart Event Prediction:

• 26 false negatives

• 523 false positives

[[3075. 26.] [ 523. 34.]]

Page 31: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Can try to improve using Lasso

result=logit_model.fit_regularized()

Page 32: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Can try to improve selecting only columns with high P-values

Optimization terminated successfully. Current function value: 0.423769 Iterations 6 Results: Logit ================================================================ Model: Logit Pseudo R-squared: 0.007 Dependent Variable: TenYearCHD AIC: 3114.2927 Date: 2020-07-12 18:18 BIC: 3157.7254 No. Observations: 3658 Log-Likelihood: -1550.1 Df Model: 6 LL-Null: -1560.6 Df Residuals: 3651 LLR p-value: 0.0019166 Converged: 1.0000 Scale: 1.0000 No. Iterations: 6.0000 ---------------------------------------------------------------- Coef. Std.Err. z P>|z| [0.025 0.975] ---------------------------------------------------------------- currentSmoker 0.0390 0.0908 0.4291 0.6679 -0.1391 0.2170 BPMeds 0.5145 0.2200 2.3388 0.0193 0.0833 0.9457 prevalentStroke 0.7716 0.4708 1.6390 0.1012 -0.1511 1.6944 prevalentHyp 0.8892 0.0983 9.0439 0.0000 0.6965 1.0818 diabetes 1.4746 0.2696 5.4688 0.0000 0.9461 2.0030 totChol -0.0067 0.0007 -9.7668 0.0000 -0.0081 -0.0054 glucose -0.0061 0.0019 -3.2113 0.0013 -0.0098 -0.0024 ================================================================

Page 33: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Stats-Models

• Select the columns

• Get a better (?) confusion matrix:

• False negatives has gone down

• False positives has gone up

cols = ['currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes', 'totChol','glucose']

[[3086. 15.] [ 549. 8.]]

Page 34: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Scikit-learn

• Import from sklearn

from sklearn.linear_model import LogisticRegression from sklearn import metrics from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report

Page 35: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Scikit-learn

• Create a logistic regression object and fit it on the data

logreg = LogisticRegression() logreg.fit(X=df[cols], y = df.TenYearCHD) y_pred = logreg.predict(df[cols]) confusion_matrix = confusion_matrix(df.TenYearCHD, y_pred) print(confusion_matrix)

Page 36: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Scikit-learn

• Scikit-learn uses a different algorithm

• Confusion matrix on the whole set is

• [[3087 14] [ 535 22]]

Page 37: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Scikit-learn

• Can also divide the set in training and test set

X_train, X_test, y_train, y_test = train_test_split(df[cols], df.TenYearCHD, test_size=0.3, random_state=0) logreg.fit(X_train, y_train) y_pred = logreg.predict(X_test) confusion_matrix = confusion_matrix(y_test, y_pred) print(confusion_matrix)

Page 38: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Logistic Regression in Scikit-learn

• Confusion matrix

• [[915 1] [176 6]]

Page 39: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Measuring Success precision recall f1-score support

0 0.84 1.00 0.91 916 1 0.86 0.03 0.06 182

accuracy 0.84 1098 macro avg 0.85 0.52 0.49 1098 weighted avg 0.84 0.84 0.77 1098

Page 40: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Measuring Success• How can we measure accuracy?

• accuracy = (fp+fn)/(tp+tn+fp+fn)

• Unfortunately, because of skewed data sets, often very high

• precision = tp/(tp+fp)

• recall = tp/(tp+fn)

• F measure = harmonic mean of precision and recall

Page 41: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Probit Regression• Instead of using the logistic function , can also use the

cumulative distribution function of the normal distribution

• Predictor is then

σ

erf(z) =2

π ∫z

0exp(−t2)dt

12 (1+erf(b + w1x1 + w2x2 + … + wnxn))

Page 42: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Probit Regression

-10 -5 5 10

0.2

0.4

0.6

0.8

1.0

�probit

Page 43: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Probit Regression• Calculations with probit are more involved

• Statsmodels implements it

• Fit the probit model

from statsmodels.discrete.discrete_model import Probit

probit_model=Probit(df.TenYearCHD,df[cols]) result=probit_model.fit()

print(result.summary()) print(result.pred_table()) for i in range(20): print(df.TenYearCHD.iloc[i], result.predict(df[cols]).iloc[i])

Page 44: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Probit Regression• Confusion matrix is now

• More false positives but less false negatives

[[3085. 16.] [ 547. 10.]]

Page 45: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Multinomial Logistic Regression

• Want to predict one of several categories based on feature vector

• Use the softmax function

softmax (z1, z2, …, zm) = ( ez1

∑mi=1 ezi

,ez2

∑mi=1 ezi

, …,ezm

∑mi=1 ezi )

Page 46: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Multinomial Logistic Regression

• Learning is still possible, but more complicated

Page 47: Logistic Regression€¦ · Logistic Regression • Combine with linear regression to obtain logistic regression approach: • Learn best weights in • • We know interpret this

Multinomial Logistic Regression

model1 = LogisticRegression(random_state=0, multi_class='multinomial', penalty='none', solver='newton-cg').fit(X_train, y_train) preds = model1.predict(X_test)


Top Related