# AI News, Using Logistic Regression in Python for Data Science

## Using Logistic Regression in Python for Data Science

In spite of the statistical theory that advises against it, you can actually try to classify a binary class by scoring one class as 1 and the other as 0.

Thanks to the following formula, you can transform a linear regression numeric estimate into a probability that is more apt to describe how a class fits an observation: probability of a class = exp(r) / (1+exp(r)) r

Using the Iris dataset from the Scikit-learn datasets module, you can use the values 0, 1, and 2 to denote three classes that correspond to three species: To make the example easier to work with, leave a single value out so that later you can use this value to test the efficacy of the logistic regression model on it.

Contrary to linear regression, logistic regression doesn&#8217;t just output the resulting class (in this case, the class 2), but it also estimates the probability of the observation&#8217;s being part of all three classes.

This is especially useful for medical purposes: Ranking a prediction in terms of likelihood with respect to others can reveal what patients are at most risk of getting or already having a disease.

Most algorithms provided by Scikit-learn that predict probabilities or a score for class can automatically handle multiclass problems using two different strategies: In the case of logistic regression, the default multiclass strategy is the one versus rest.

## Logistic Regression¶

There is a great math explanation in chapter 3 of Michael Neilson’s deep learning book [5], but for now I’ll simply say it’s because our prediction function is non-linear (due to sigmoid transform).

The corollary is increasing prediction accuracy (closer to 0 or 1) has diminishing returns on reducing cost due to the logistic nature of our cost function.

Above functions compressed into one Multiplying by $$y$$ and $$(1-y)$$ in the above equation is a sneaky trick that let’s us use the same equation to solve for both y=1 and y=0 cases.

## Logistic Regression for Machine Learning

The logistic function, also called the sigmoid function was developed by statisticians to describe properties of population growth in ecology, rising quickly and maxing out at the carrying capacity of the environment.

/ (1 + e^-value) Where e is the base of the natural logarithms (Euler&#8217;s number or the EXP() function in your spreadsheet) and value is the actual numerical value that you want to transform.

= e^(b0 + b1*x) / (1 + e^(b0 + b1*x)) Where y is the predicted output, b0 is the bias or intercept term and b1 is the coefficient for the single input value (x).

For example, if we are modeling people&#8217;s sex as male or female from their height, then the first class could be male and the logistic regression model could be written as the probability of male given a person&#8217;s height, or more formally: P(sex=male|height) Written another way, we are modeling the probability that an input (X) belongs to the default class (Y=1), we can write this formally as: P(X) = P(Y=1|X) We&#8217;re predicting probabilities?

The impact of this is that we can no longer understand the predictions as a linear combination of the inputs as we can with linear regression, for example, continuing on from above, the model can be stated as: p(X) = e^(b0 + b1*X) / (1 + e^(b0 + b1*X)) I

don&#8217;t want to dive into the math too much, but we can turn around the above equation as follows (remember we can remove the e from one side by adding a natural logarithm (ln) to the other): ln(p(X) / 1 &#8211;

p(X)) = b0 + b1 * X This is useful because we can see that the calculation of the output on the right is linear again (just like linear regression), and the input on the left is a log of the probability of the default class.

So we could instead write: ln(odds) = b0 + b1 * X Because the odds are log transformed, we call this left hand side the log-odds or the probit.

It is possible to use other types of functions for the transform (which is out of scope_, but as such it is common to refer to the transform that relates the linear regression equation to the probabilities as the link function, e.g.

We can move the exponent back to the right and write it as: odds = e^(b0 + b1 * X) All of this helps us understand that indeed the model is still a linear combination of the inputs, but that this linear combination relates to the log-odds of the default class.

The intuition for maximum-likelihood for logistic regression is that a search procedure seeks values for the coefficients (Beta values) that minimize the error in the probabilities predicted by the model to those in the data (e.g.

## Logistic regression

In statistics, the logistic model (or logit model) is a widely used statistical model that, in its basic form, uses a logistic function to model a binary dependent variable;

In the logistic model, the log-odds (the logarithm of the odds) for the value labeled '1' is a linear combination of one or more independent variables ('predictors');

the independent variables can each be a binary variable (two classes, coded by an indicator variable) or a continuous variable (any real value).

The corresponding probability of the value labeled '1' can vary between 0 (certainly the value '0') and 1 (certainly the value '1'), hence the labeling;

the defining characteristic of the logistic model is that increasing one of the independent variables multiplicatively scales the odds of the given outcome at a constant rate, with each dependent variable having its own parameter;

The binary logistic regression model has extensions to more than two levels of the dependent variable: categorical outputs with more than two values are modelled by multinomial logistic regression, and if the multiple categories are ordered, by ordinal logistic regression, for example the proportional odds ordinal logistic model.&#91;1&#93;

The model itself simply models probability of output in terms of input, and does not perform statistical classification (it is not a classifier), though it can be used to make a classifier, for instance by choosing a cutoff value and classifying inputs with probability greater than the cutoff as one class, below the cutoff as the other;

coronary heart disease), based on observed characteristics of the patient (age, sex, body mass index, results of various blood tests, etc.).&#91;8&#93;&#91;9&#93;

Another example might be to predict whether an Indian voter will vote BJP or Trinamool Congress or Left Front or Congress, based on age, income, sex, race, state of residence, votes in previous elections, etc.&#91;10&#93;

In economics it can be used to predict the likelihood of a person's choosing to be in the labor force, and a business application would be to predict the likelihood of a homeowner defaulting on a mortgage.

One may begin to understand logistic regression by first considering a logistic model with given parameters, then seeing how the coefficients can be estimated ('regressed') from data.

1

2

these may be continuous variables (taking a real number as value), or indicator functions for binary variables (taking value 0 or 1).

i

1

2

0

since happening o of the time and not happening 1 time corresponds to happening o times out of a total of o + 1 events, so the corresponding probability is:

1

2

1

2

3

4

The reason for using logistic regression for this problem is that the values of the dependent variable, pass and fail, while represented by '1' and '0', are not cardinal numbers.

If the problem was changed so that pass/fail was replaced with the grade 0–100 (cardinal numbers), then simple regression analysis could be used.

The table shows the number of hours each student spent studying, and whether they passed (1) or failed (0).

The graph shows the probability of passing the exam versus the number of hours studying, with the logistic regression curve fitted to the data.

One additional hour of study is estimated to increase log-odds of passing by 1.5046, so multiplying odds of passing by

The form with the x-intercept (2.71) shows that this estimates even odds (log-odds 0, odds 1, probability 1/2) for a student who studies 2.71 hours.

1

1

+

exp

&#x2061;

(

&#x2212;

(

1.5046

&#x22C5;

2

&#x2212;

4.0777

)

)

1

1

+

exp

&#x2061;

(

&#x2212;

(

1.5046

&#x22C5;

4

&#x2212;

4.0777

)

)

Binomial or binary logistic regression deals with situations in which the observed outcome for a dependent variable can have only two possible types, '0' and '1' (which may represent, for example, 'dead' vs.

If a particular observed outcome for the dependent variable is the noteworthy possible outcome (referred to as a 'success' or a 'case') it is usually coded as '1' and the contrary outcome (referred to as a 'failure' or a 'noncase') as '0'.

Unlike ordinary linear regression, however, logistic regression is used for predicting dependent variables that take membership in one of a limited number of categories (treating the dependent variable in the binomial case as the outcome of a Bernoulli trial) rather than a continuous outcome.

To do that, binomial logistic regression first calculates the odds of the event happening for different levels of each independent variable, and then takes its logarithm to create a continuous criterion as a transformed version of the dependent variable.

The predicted value of the logit is converted back into predicted odds via the inverse of the natural logarithm, namely the exponential function.

Thus, although the observed dependent variable in binary logistic regression is a zero-or-one variable, the logistic regression estimates the odds, as a continuous variable, that the dependent variable is a success (a case).

this categorical prediction can be based on the computed odds of a success, with predicted odds above some chosen cutoff value being translated into a prediction of a success.

Logistic regression measures the relationship between the categorical dependent variable and one or more independent variables by estimating probabilities using a logistic function, which is the cumulative logistic distribution.

Thus, it treats the same set of problems as probit regression using similar techniques, with the latter using a cumulative normal distribution curve instead.

Equivalently, in the latent variable interpretations of these two methods, the first assumes a standard logistic distribution of errors and the second a standard normal distribution of errors.&#91;16&#93;

Second, the predicted values are probabilities and are therefore restricted to (0,1) through the logistic distribution function because logistic regression predicts the probability of particular outcomes rather than the outcomes themselves.

0

1

Instead they are to be found by an iterative search process, usually implemented by a software program, that finds the maximum of a complicated 'likelihood expression' that is a function of all of the observed

i

i

i

Given that the logit ranges between negative and positive infinity, it provides an adequate criterion upon which to conduct linear regression and the logit is easily converted back into the odds.&#91;14&#93;

1

&#x03B2;

1

a

d

b

c

0

1

0

1

1

2

2

m

m

0

i

=

1

m

i

i

{\displaystyle \beta _{0}+\beta _{1}x_{1}+\beta _{2}x_{2}+\cdots +\beta _{m}x_{m}=\beta _{0}+\sum _{i=1}^{m}\beta _{i}x_{i}}

Then when this is used in the equation relating the logged odds of a success to the values of the predictors, the linear regression will be a multiple regression with m explanators;

j

y

(

1

&#x2212;

y

)

&#x2212;

1

widely used rule of thumb, the 'one in ten rule', states that logistic regression models give stable values for the explanatory variables if based on a minimum of about 10 events per explanatory variable (EPV);

with the authors stating 'If we (somewhat subjectively) regard confidence interval coverage less than 93 percent, type I error greater than 7 percent, or relative bias greater than 15 percent as problematic, our results indicate that problems are fairly frequent with 2–4 EPV, uncommon with 5–9 EPV, and still observed with 10–16 EPV.

A useful criterion is whether the fitted model will be expected to achieve the same predictive discrimination in a new sample as it appeared to achieve in the model development sample.

Also, one can argue that 96 observations are needed only to estimate the model's intercept precisely enough that the margin of error in predicted probabilities is ±0.1 with an 0.95 confidence level&#91;15&#93;.

Unlike linear regression with normally distributed residuals, it is not possible to find a closed-form expression for the coefficient values that maximize the likelihood function, so that an iterative process must be used instead;

This process begins with a tentative solution, revises it slightly to see if it can be improved, and repeats this revision until no more improvement is made, at which point the process is said to have converged.&#91;26&#93;

A failure to converge may occur for a number of reasons: having a large ratio of predictors to cases, multicollinearity, sparseness, or complete separation.

can, for example, be calculated using iteratively reweighted least squares (IRLS), which is equivalent to minimizing the Log-likelihood of a Bernoulli distributed process using Newton's method.

w

0

1

2

1

2

1

1

+

e

&#x2212;

w

T

x

(

i

)

In linear regression analysis, one is concerned with partitioning variance via the sum of squares calculations – variance in the criterion is essentially divided into variance accounted for by the predictors and residual variance.

When a 'saturated' model is available (a model with a theoretically perfect fit), deviance is calculated by comparing a given model with the saturated model.&#91;14&#93;

The log of this likelihood ratio (the ratio of the fitted model to the saturated model) will produce a negative value, hence the need for a negative sign.

When assessed upon a chi-square distribution, nonsignificant chi-square values indicate very little unexplained variance and thus, good model fit.

When the saturated model is not available (a common case), deviance is calculated simply as −2·(log likelihood of the fitted model), and the reference to the saturated model's log likelihood can be removed from all that follows without harm.

Given that deviance is a measure of the difference between a given model and the saturated model, smaller values indicate better fit.

Thus, to assess the contribution of a predictor or set of predictors, one can subtract the model deviance from the null deviance and assess the difference on a

s

&#x2212;

p

2

If the model deviance is significantly smaller than the null deviance then one can conclude that the predictor or set of predictors significantly improved model fit.

In linear regression the squared multiple correlation, R2 is used to assess goodness of fit as it represents the proportion of variance in the criterion that is explained by the predictors.&#91;29&#93;

It represents the proportional reduction in the deviance wherein the deviance is treated as a measure of variation analogous but not identical to the variance in linear regression analysis.&#91;26&#93;

0

2

/

n

The likelihood ratio R2 is often preferred to the alternatives as it is most analogous to R2 in linear regression, is independent of the base rate (both Cox and Snell and Nagelkerke R2s increase as the proportion of cases increase from 0 to .5) and varies between 0 and 1.

The reason these indices of fit are referred to as pseudo R2 is that they do not represent the proportionate reduction in error as the R2 in linear regression does.&#91;29&#93;

2

distribution to assess whether or not the observed event rates match expected event rates in subgroups of the model population.

This test is considered to be obsolete by some statisticians because of its dependence on arbitrary binning of predicted probabilities and relative low power.&#91;32&#93;

Given that the logit is not intuitive, researchers are likely to focus on a predictor's effect on the exponential function of the regression coefficient – the odds ratio (see definition).

In logistic regression, there are several different tests designed to assess the significance of an individual predictor, most notably the likelihood ratio test and the Wald statistic.

The likelihood-ratio test discussed above to assess model fit is also the recommended procedure to assess the contribution of individual 'predictors' to a given model.&#91;14&#93;&#91;26&#93;&#91;29&#93;

In the case of a single predictor model, one simply compares the deviance of the predictor model with that of the null model on a chi-square distribution with a single degree of freedom.

If the predictor model has a significantly smaller deviance (c.f chi-square using the difference in degrees of freedom of the two models), then one can conclude that there is a significant association between the 'predictor' and the outcome.

SPSS) do provide likelihood ratio test statistics, without this computationally intensive test it would be more difficult to assess the contribution of individual predictors in the multiple logistic regression case.

To assess the contribution of individual predictors one can enter the predictors hierarchically, comparing each new model with the previous to determine the contribution of each predictor.&#91;29&#93;

The Wald statistic is the ratio of the square of the regression coefficient to the square of the standard error of the coefficient and is asymptotically distributed as a chi-square distribution.&#91;26&#93;

When the regression coefficient is large, the standard error of the regression coefficient also tends to be large increasing the probability of Type-II error.

For example, suppose there is a disease that affects 1 person in 10,000 and to collect our data we need to do a complete physical.

As a rule of thumb, sampling controls at a rate of five times the number of cases will produce sufficient control data.&#91;33&#93;

Logistic regression is unique in that it may be estimated on unbalanced data, rather than randomly sampled data, and still yield correct coefficient estimates of the effects of each independent variable on the outcome.

j

0

0

&#x03C0;

&#x007E;

xm,i (also called independent variables, predictor variables, features, or attributes), and a binary outcome variable Yi (also known as a dependent variable, response variable, output variable, or class), i.e.

The main distinction is between continuous variables (such as income, age and blood pressure) and discrete variables (such as sex or race).

Discrete variables referring to more than two possible choices are typically coded using dummy variables (or indicator variables), that is, separate explanatory variables taking the value 0 or 1 are created for each possible value of the discrete variable, with a 1 meaning 'variable does have the given value' and a 0 meaning 'variable does not have that value'.

For example, a four-way discrete variable of blood type with the possible values 'A, B, AB, O' can be converted to four separate two-way dummy variables, 'is-A, is-B, is-AB, is-O', where only one of them has the value 1 and all the rest have the value 0.

(In a case like this, only three of the four dummy variables are independent of each other, in the sense that once the values of three of the variables are known, the fourth is automatically determined.

This also means that when all four possibilities are encoded, the overall model is not identifiable in the absence of additional constraints such as a regularization constraint.

Formally, the outcomes Yi are described as being Bernoulli-distributed data, where each outcome is determined by an unobserved probability pi that is specific to the outcome at hand, but related to the explanatory variables.

The basic idea of logistic regression is to use the mechanism already developed for linear regression by modeling the probability pi using a linear predictor function, i.e.

0

m

The particular model used by logistic regression, which distinguishes it from standard linear regression and from other types of regression analysis used for binary-valued outcomes, is the way the probability of a particular outcome is linked to the linear predictor function:

This formulation expresses logistic regression as a type of generalized linear model, which predicts variables with various types of probability distributions by fitting a linear predictor function of the above form to some sort of arbitrary transformation of the expected value of the variable.

It also has the practical effect of converting the probability (which is bounded to be between 0 and 1) to a variable that ranges over

that give the most accurate predictions for the data already observed), usually subject to regularization conditions that seek to exclude unlikely values, e.g.

(Regularization is most commonly done using a squared regularizing function, which is equivalent to placing a zero-mean Gaussian prior distribution on the coefficients, but other regularizers are also possible.) Whether or not regularization is used, it is usually not possible to find a closed-form solution;

instead, an iterative numerical method must be used, such as iteratively reweighted least squares (IRLS) or, more commonly these days, a quasi-Newton method such as the L-BFGS method.

The interpretation of the βj parameter estimates is as the additive effect on the log of the odds for a unit change in the jth explanatory variable.

This formulation is common in the theory of discrete choice models, and makes it easier to extend to certain more complicated models with multiple, correlated choices, as well as to compare logistic regression to the closely related probit model.

the latent variable can be written directly in terms of the linear predictor function and an additive random error variable that is distributed according to a standard logistic distribution.

The choice of modeling the error variable specifically with a standard logistic distribution, rather than a general logistic distribution with the location and scale set to arbitrary values, seems restrictive, but in fact it is not.

It must be kept in mind that we can choose the regression coefficients ourselves, and very often can use them to offset changes in the parameters of the error variable's distribution.

For example, a logistic error-variable distribution with a non-zero location parameter μ (which sets the mean) is equivalent to a distribution with a zero location parameter, where μ has been added to the intercept coefficient.

Similarly, an arbitrary scale parameter s is equivalent to setting the scale parameter to 1 and then dividing all regression coefficients by s.

In the latter case, the resulting value of Yi* will be smaller by a factor of s than in the former case, for all sets of explanatory variables — but critically, it will always remain on the same side of 0, and hence lead to the same Yi choice.

It turns out that this formulation is exactly equivalent to the preceding one, phrased in terms of the generalized linear model and without any latent variables.

This can be shown as follows, using the fact that the cumulative distribution function (CDF) of the standard logistic distribution is the logistic function, which is the inverse of the logit function, i.e.

Then: This formulation—which is standard in discrete choice models—makes clear the relationship between logistic regression (the 'logit model') and the probit model, which uses an error variable distributed according to a standard normal distribution instead of a standard logistic distribution.

The only difference is that the logistic distribution has somewhat heavier tails, which means that it is less sensitive to outlying data (and hence somewhat more robust to model mis-specifications or erroneous data).

Then This model has a separate latent variable and a separate set of regression coefficients for each possible outcome of the dependent variable.

The reason for this separation is that it makes it easy to extend logistic regression to multi-outcome categorical variables, as in the multinomial logit model.

It is also possible to motivate each of the separate latent variables as the theoretical utility associated with making the associated choice, and thus motivate logistic regression in terms of utility theory.

(In terms of utility theory, a rational actor always chooses the choice with the greatest associated utility.) This is the approach taken by economists when formulating discrete choice models, because it both provides a theoretically strong foundation and facilitates intuitions about the model, which in turn makes it easy to consider various sorts of extensions.

The choice of the type-1 extreme value distribution seems fairly arbitrary, but it makes the mathematics work out, and it may be possible to justify its use through rational choice theory.

It turns out that this model is equivalent to the previous model, although this seems non-obvious, since there are now two sets of regression coefficients and error variables, and the error variables have a different distribution.

An intuition for this comes from the fact that, since we choose based on the maximum of two values, only their difference matters, not the exact values — and this effectively removes one degree of freedom.

1

0

As an example, consider a province-level election where the choice is between a right-of-center party, a left-of-center party, and a secessionist party (e.g.

explanatory variable) has in contributing to the utility — or more correctly, the amount by which a unit change in an explanatory variable changes the utility of a given choice.

On the other hand, the left-of-center party might be expected to raise taxes and offset it with increased welfare and other assistance for the lower and middle classes.

This would cause significant positive benefit to low-income people, perhaps weak benefit to middle-income people, and significant negative benefit to high-income people.

A low-income or middle-income voter might expect basically no clear utility gain or loss from this, but a high-income voter might expect negative utility, since he/she is likely to own companies, which will have a harder time doing business in such an environment and probably lose money.

Yet another formulation combines the two-way latent variable formulation above with the original formulation higher up without latent variables, and in the process provides a link to one of the standard formulations of the multinomial logit.

Note that two separate sets of regression coefficients have been introduced, just as in the two-way latent variable model, and the two equations appear a form that writes the logarithm of the associated probability as a linear predictor, with an extra term

i

As a result, the model is nonidentifiable, in that multiple combinations of β0 and β1 will produce the same probabilities for all possible explanatory variables.

Note that most treatments of the multinomial logit model start out either by extending the 'log-linear' formulation presented here or the two-way latent variable formulation presented above, since both clearly show the way that the model could be extended to multi-way outcomes.

In general, the presentation with latent variables is more common in econometrics and political science, where discrete choice models and utility theory reign, while the 'log-linear' formulation here is more common in computer science, e.g.

closely related model assumes that each i is associated not with a single Bernoulli trial but with ni independent identically distributed trials, where the observation Yi is the number of successes observed (the sum of the individual Bernoulli-distributed random variables), and hence follows a binomial distribution:

However, when the sample size or the number of parameters is large, full Bayesian simulation can be slow, and people often use approximate methods such as variational Bayes and expectation propagation.

Logistic Regression Using Excel

Predict who survives the Titanic disaster using Excel. Logistic regression allows us to predict a categorical outcome using categorical and numeric data.

Logistic Regression - Fun and Easy Machine Learning

Logistic Regression - Fun and Easy Machine Learning

How MLE (Maximum Likelihood Estimation) algorithm works

In this video I show how the MLE algorithm works. We provide an animation where several points are classified considering three classes with mean and ...

GLM in R: logistic regression example

Basic interpretation of output of logistic regression covering: slope coefficient, Z- value, Null Deviance, Residual Deviance.

Linear Regression and Correlation - Example

Course web page:

ROC Curves and Area Under the Curve (AUC) Explained

Transcript and screenshots: Visualization: Research paper: .

StatQuest: Linear Discriminant Analysis (LDA) clearly explained.

LDA is surprisingly simple and anyone can understand it. Here I avoid the complex linear algebra and use illustrations to show you what it does so you will know ...

Logistic Regression - Predicted Probabilities (part 1)

I demonstrate how to calculate predicted probabilities and group membership for cases in a binary (a.k.a., binomial) logistic regression analysis. I do so through ...

Model Fitting and Regression in MATLAB

Demonstrates how to model a curve and perform regression in Matlab. Made by faculty at the University of Colorado Boulder Department of Chemical and ...

Random Forest in R - Classification and Prediction Example with Definition & Steps

Provides steps for applying random forest to do classification and prediction. R code file: Data: Machine Learning .