A Complete Guide to Logistic Regression using Python and Excel
Introduction
Logistic regression is one of the most popular and widely used machine learning algorithms for classification problems. Whether you want to predict if an email is spam or not, assess the likelihood of a customer churning, or determine if a financial transaction is fraudulent, logistic regression is a powerful tool to have in your data science toolkit.
In this comprehensive guide, we will dive deep into the fundamentals of logistic regression, understand how it works mathematically, and walk through step-by-step tutorials on implementing it in both Python and Excel. By the end, you will have a solid grasp of logistic regression and be equipped to apply it to real-world problems. Let‘s get started!
What is Logistic Regression?
Logistic regression is a statistical method for predicting binary outcomes. Unlike linear regression which outputs continuous number values, logistic regression transforms its output using the logistic sigmoid function to return a probability value which can then be mapped to two or more discrete classes.
Mathematically, logistic regression models the probabilities for classification problems with two possible outcomes. It does this by fitting data to a logit function. A logit function is simply a log of odds, where odds are a ratio of probabilities ( p / (1-p) ).
logit(p) = log(p / (1-p)) = b0 + b1*X
The coefficients b0 and b1 are estimated during the model fitting process using maximum-likelihood estimation. Once the coefficients are found, the logistic regression equation can be used to calculate the probability of a binary event occurring. If the probability is greater than 0.5, the outcome is classified as 1 (or True), otherwise it is 0 (or False).
An important point about logistic regression is that the dependent variable is binary or dichotomous – it only contains data coded as 1 or 0. So the goal of logistic regression is to determine the likelihood that a data point belongs to the default class (usually 0). This can then be used to classify data points based on their features.
When to Use Logistic Regression
Logistic regression is used when the dependent variable (target) is categorical. For example:
- To predict whether an email is spam (1) or not spam (0)
- Whether the tumor is malignant (1) or not (0)
- Whether the customer will churn (1) or not (0)
It is a classification algorithm, so the output should be a category. If the output is a continuous value, then linear regression should be used instead. Logistic regression is extensively used in different fields including medical, marketing, e-commerce and finance. Some common application examples include:
- Predicting risk of developing a certain disease (e.g. diabetes, heart disease, cancer)
- Estimating likelihood of a homeowner defaulting on a mortgage
- Classifying a transaction as fraudulent
- Predicting if a user will click on an online advertisement
Assumptions of Logistic Regression
While logistic regression does not have many of the key assumptions of linear regression, it does have some. Before applying logistic regression to a classification problem, these assumptions should be checked:
-
Binary logistic regression requires the dependent variable to be binary and ordinal logistic regression requires the dependent variable to be ordinal.
-
Logistic regression requires the observations to be independent of each other. In other words, the observations should not come from repeated measurements or matched data.
-
Logistic regression requires there to be little or no multicollinearity among the independent variables. This means that the independent variables should not be too highly correlated with each other.
-
Logistic regression assumes linearity of independent variables and log odds. although this analysis does not require the dependent and independent variables to be related linearly, it requires that the independent variables are linearly related to the log odds.
If any of these assumptions are not met, you may need to make adjustments or select a different modeling technique. The good news is that even if some of the assumptions are not met exactly, logistic regression is somewhat robust to violations and may still provide useful results.
Interpreting Logistic Regression Coefficients
Interpreting a logistic regression model can seem tricky at first, since it outputs a probability between 0 and 1. However, the coefficients can be interpreted as change in log(odds) for a one unit change in the independent variable, holding other variables constant.
The odds are simply the probability of an event divided by the probability of the event not occurring. So an odds ratio compares the odds for two different values of the independent variable. The odds ratio for a coefficient tells you how the odds change for a one unit change in the independent variable.
For example, if the coefficient for variable x1 is 1.2, then a one unit change in x1 is associated with a exp(1.2) = 3.32 change in odds, holding all other variables constant. So increasing x1 by one unit increases the odds of the event by a factor of 3.32. This could be interpreted as a one unit increase in x1 being associated with the event being 3.32 times more likely, holding other variables constant.
Coefficients in logistic regression are log(odds). To find the odds ratio, you need to take the exponential of the coefficient. The odds ratio can then be interpreted as the change in odds of being in the dependent variable category for a one unit change in the independent variable.
Logistic Regression Tutorial using Python
Now that we understand the theory behind logistic regression, let‘s see how to implement it in Python. We will go through a step-by-step tutorial covering data preprocessing, splitting data into train and test sets, model training, evaluation, and visualization.
For this tutorial, we will use the popular iris flower dataset. This dataset contains measurements for 150 iris flowers from three different species. We will use logistic regression to predict the species of a flower based on its measurements. Here are the steps:
Step 1: Import required libraries
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
import matplotlib.pyplot as plt
Step 2: Load the dataset
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
Step 3: Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
Step 4: Train the model
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
Step 5: Evaluate performance on test set
y_pred = logreg.predict(X_test)
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
Step 6: Visualize decision boundary
X_new = x1_new, x2_new, x3_new, x4_new = plt.meshgrid(np.linspace(0, 8, 200).reshape(-1, 1),
np.linspace(0, 4.5, 200).reshape(-1, 1),
np.linspace(0, 7, 200).reshape(-1, 1),
np.linspace(0, 3, 200).reshape(-1, 1))
X_new_reshape = np.c_[x1_new.ravel(), x2_new.ravel(), x3_new.ravel(), x4_new.ravel()]
y_proba = logreg.predict_proba(X_new_reshape)[:, 1].reshape(x1_new.shape)
plt.figure(figsize=(10, 8))
plt.contourf(x1_new, x2_new, y_proba, 20, cmap=‘RdBu‘, alpha=0.5)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap=‘autumn‘, edgecolors=‘k‘)
plt.xlabel(‘Sepal length‘)
plt.ylabel(‘Sepal width‘)
plt.colorbar()
plt.show()
The confusion matrix and classification report show that the model achieves 98% accuracy on the test set. The visualization also shows a clear decision boundary separating the three iris flower classes based on sepal length and width.
Logistic Regression in Excel
Logistic regression can also be performed in Excel, although it is a bit more limited than using Python. Here are the basic steps:
- Organize your data in columns, with the independent variables in the first columns and the binary dependent variable in the last column.
- Click on the "Data" tab and select "Data Analysis" (if you don‘t see it, you may need to install the Analysis ToolPak first).
- Scroll down and select "Regression" and click "OK".
- For "Input Y Range", select your binary dependent variable column.
- For "Input X Range", select your independent variable columns.
- Check the "Labels" box if you have column headers.
- Select an output location for your results and click "OK".
The output will contain a summary report with the logistic regression coefficients. To interpret them, you need to calculate the exponential of the coefficients to get the odds ratio. A positive coefficient means the odds increase and a negative coefficient means the odds decrease.
While Excel can be used for simple logistic regression models, it becomes unwieldy for more complex analyses. It also lacks many of the diagnostic and evaluation tools available in statistical software like Python. For most real-world applications, it is recommended to use a programming language like Python or R.
Advantages and Limitations of Logistic Regression
Some of the key advantages of logistic regression include:
- It is a simple and efficient algorithm that performs well on linearly separable classes.
- It provides easy to interpret results in the form of probabilities and odds ratios.
- It does not make any assumptions about the distribution of the features (e.g. they do not have to be normally distributed).
- Logistic regression handles non-linear effects since it applies a non-linear log transformation to the predicted odds ratio.
- It is relatively robust to outliers and does not require the dependent and independent variables to be related linearly.
However, logistic regression also has some important limitations:
- It assumes linearity between the dependent variable and the independent variables.
- It requires the dependent variable to be binary or ordinal for logistic regression to be meaningful.
- It is sensitive to high correlations among the independent variables (multicollinearity).
- It may not perform well on datasets with many features, since it cannot handle a large number of categorical features/variables.
- It tends to underperform when there are multiple or non-linear decision boundaries.
Extensions and Alternatives to Logistic Regression
There are a few common techniques for extending logistic regression:
-
Regularization: Regularization methods like Lasso and Ridge can be used to penalize model complexity and reduce overfitting. These add a regularization term to the loss function that shrinks the coefficient estimates towards zero.
-
Multinomial Logistic Regression: Multinomial (or multi-class) logistic regression extends binary logistic regression to problems where the dependent variable can take on more than two classes.
-
Ordinal Logistic Regression: Ordinal logistic regression extends binary logistic regression to handle ordinal dependent variables (i.e. variables that have ordered categories).
There are also a number of alternative algorithms you may want to consider for classification problems:
- Decision Trees and Random Forests
- Support Vector Machines
- Naive Bayes
- K-Nearest Neighbors
- Neural Networks
The choice of algorithm depends on the size and structure of your data, interpretability requirements, and performance goals. It‘s always good practice to try multiple algorithms and compare their results on a validation set before making a final selection.
Conclusion
Logistic regression is a powerful and widely used algorithm for binary classification problems. It is simple to implement and provides an easy to interpret output in the form of probabilities and odds ratios.
In this guide, we covered the fundamentals of how logistic regression works, when to use it, and how to implement it in Python and Excel. We walked through the key steps of data preparation, model training and evaluation, and result visualization and interpretation.
We also discussed some of the assumptions and limitations of logistic regression, as well as extensions like regularization and multinomial or ordinal logistic regression. Finally, we briefly touched on some alternative classification algorithms to consider.
To learn more about logistic regression and other machine learning techniques, check out the following resources:
- Hands-On Machine Learning with Scikit-Learn and TensorFlow
- An Introduction to Statistical Learning
- Applied Predictive Modeling
- Logistic Regression: A Primer
With this foundation, you are well equipped to start applying logistic regression to real-world classification problems. Remember that machine learning is an iterative process – you will often need to try multiple algorithms, fine-tune your model parameters, and continuously evaluate performance to achieve the best results. Happy modeling!