Skip to content

Logistic Regression Explained with Examples Narender Kumar Spark By {Examples}

  • by

Logistic Regression is one of the most widely used Artificial Intelligence algorithms in real-life Machine Learning problems — thanks to its simplicity, interpretability, and speed. In the next few minutes we’ll understand what’s behind the working of this algorithm.

In this article, I will explain logistic regression with some data, python examples, and output.

1. What is Logistic Regression

Logistic regression is a statistical method used to model the relationship between a binary dependent variable and one or more independent variables. The goal of logistic regression is to predict the probability of an event occurring based on a set of predictor variables.

In logistic regression, the dependent variable is binary, meaning it can only take on two values, typically labeled as 0 or 1. The independent variables can be either continuous or categorical.

The logistic regression model is based on the logistic function, which is a type of S-shaped curve that maps any continuous input to a probability value between 0 and 1. The logistic function allows us to model the relationship between the independent variables and the probability of the dependent variable taking on the value of 1.

The logistic regression model estimates the coefficients of the independent variables that are most predictive of the dependent variable. These coefficients are used to create a linear equation that is then transformed by the logistic function to produce a probability value for the dependent variable taking on the value of 1.

Logistic regression is commonly used in fields such as healthcare, marketing, finance, and social sciences to predict the likelihood of an event occurring, such as whether a patient has a certain disease or whether a customer will buy a product.

1.1 The equation for logistic regression is:

# Logistic Regression Equation
p = 1 / (1 + e^(-z))

where p is the probability of the dependent variable taking on the value of 1, z is a linear combination of the independent variables and their coefficients.

The equation can be expanded as follows:

z = b0 + b1<em>x1 + b2</em>x2 + … + bn*xn

where b0 is the intercept term, b1, b2, …, bn are the coefficients of the independent variables x1, x2, …, xn, respectively.

1.2 Why the name Logistic Regression?

The name “logistic regression” comes from the function used in the model, which is called the logistic function or sigmoid function. The logistic function is a mathematical function that takes any input value and returns an output value between 0 and 1.

The name “logistic” comes from the term “logit”, which is the logarithm of the odds of an event occurring. In logistic regression, the logit of the probability is modeled as a linear combination of the independent variables, and then the logistic function is applied to the result to obtain the predicted probability.

In summary, the name “logistic regression” refers to the use of the logistic function to model the relationship between the independent variables and the probability of an event occurring.

2. Evaluation Metrics for Logistic Regression?

Logistic regression is a popular machine learning algorithm used for binary classification problems. To evaluate the performance of a logistic regression model, various evaluation metrics can be used. Here are some common evaluation metrics for logistic regression:

Assume we have a binary classification problem and we are given the predicted probabilities and the true labels for a set of instances:

2.1 Accuracy:

Accuracy measures the percentage of correctly classified instances out of all instances. While accuracy is a commonly used metric, it can be misleading if the data is imbalanced, meaning one class is much more frequent than the other.

Python Code For Accuracy

2.2 Precision:

Precision measures the proportion of true positive predictions out of all positive predictions. It focuses on the model’s ability to correctly identify positive cases.

Python Code For Precision

2.3 Recall:

Recall measures the proportion of true positive predictions out of all actual positive cases. It focuses on the model’s ability to identify all positive cases, even if it results in some false positives.

Python Code For Recall

2.4 F1 Score

The F1 score is the harmonic mean of precision and recall. It combines the two metrics to provide a more balanced evaluation of the model’s performance.

Python Code For F1 Score

2.5 Area Under the Receiver Operating Characteristic Curve (AUC-ROC):

The AUC-ROC measures the model’s ability to distinguish between the positive and negative classes. It is a measure of the tradeoff between the true positive rate and the false positive rate.

Python Code For AUC-ROC

2.6 Confusion Matrix:

A confusion matrix is a table that summarizes the performance of a classification model. It shows the number of true positives, true negatives, false positives, and false negatives.

Python Code For Confusion Matrix

These evaluation metrics provide different perspectives on the performance of a logistic regression model. The choice of evaluation metric depends on the specific problem and the relative importance of precision and recall. It’s important to select an appropriate evaluation metric that aligns with the specific business needs and goals of the problem at hand.

3. Code example with Data and Output For Logistic Regression

## Import The libraries
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, log_loss
from sklearn.model_selection import train_test_split

# Load iris dataset
iris = load_iris()

# Set features and target
X = iris.data
y = iris.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a logistic regression model
clf = LogisticRegression(random_state=42)
clf.fit(X_train, y_train)

# Make predictions on the testing set
y_pred = clf.predict(X_test)

# Compute evaluation metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average=’weighted’)
recall = recall_score(y_test, y_pred, average=’weighted’)
f1 = f1_score(y_test, y_pred, average=’weighted’)
auc_roc = roc_auc_score(y_test, clf.predict_proba(X_test), multi_class=’ovr’)
logloss = log_loss(y_test, clf.predict_proba(X_test))

# Print evaluation metrics
print(‘Accuracy:’, accuracy)
print(‘Precision:’, precision)
print(‘Recall:’, recall)
print(‘F1 score:’, f1)
print(‘AUC-ROC:’, auc_roc)
print(‘Log Loss:’, logloss)

# Output:

Accuracy: 1.0
Precision: 1.0
Recall: 1.0
F1 score: 1.0
AUC-ROC: 1.0
Log Loss: 0.04542674063376945

In this example, we first loaded the iris dataset and split it into training and testing sets. We then trained a logistic regression model on the training set and made predictions on the testing set. Finally, we computed the evaluation metrics for the model and printed the results.

The output shows that the model performed perfectly on the iris dataset, achieving perfect accuracy, precision, recall, and F1 score, as well as an AUC-ROC score of 1.0. The log loss value of 0.0454 indicates that the model’s predicted probabilities were very close to the true probabilities.

4. Key benefits of Logistic regression

Logistic regression is a widely used statistical and machine learning technique with several key benefits, including:

4.1 Interpretable results:

Logistic regression models provide coefficients for each feature, indicating the direction and magnitude of the impact on the outcome variable. This makes it easy to interpret the results and understand the factors that are driving the predictions.

4.2 Fast and efficient:

Logistic regression is a relatively simple algorithm and can be trained quickly on large datasets. It is also computationally efficient and can be used to make predictions in real-time.

4.3 Robust to noise:

Logistic regression is less susceptible to overfitting than other machine learning algorithms. This is because it has a simpler hypothesis space and fewer parameters to estimate.

4.4 Can handle both binary and continuous variables:

Logistic regression can handle both categorical (binary) and continuous variables, making it a versatile tool for a wide range of applications.

4.5 Works well with small datasets:

Logistic regression can be used to model small datasets and still produce accurate predictions.

4.6 Can be regularized:

Logistic regression can be regularized to prevent overfitting and improve generalization performance.

5. Assumptions Of the Logistic Regression

Logistic regression is a widely used statistical and machine learning technique for binary classification problems. Like all statistical models, logistic regression is based on certain assumptions. Here are the key assumptions of logistic regression:

5.1 Linearity of the logit:

Logistic regression assumes that the logit of the outcome variable is a linear combination of the predictor variables. This means that the relationship between the predictors and the outcome is log-linear.

Python Code To Check Linearity of the logit

5.2 Independence of errors:

The errors in logistic regression should be independent of each other, which means that there should be no correlation between the errors in the model. This assumption is important because correlated errors can bias the estimates of the model parameters.

Code Example To Check Independence Of Errors

To check for independence of errors, you can create a residual plot of the predicted probabilities against the residuals. The plot should show no pattern in the residuals, indicating that the errors are independent

5.3 Absence of multicollinearity:

Logistic regression assumes that there is no high correlation between the predictor variables. This is because multicollinearity can lead to unstable estimates of the model parameters.

Code Example To Check Independence Of Errors Absence of multicollinearity

5.4 No outliers:

Outliers in the data can have a disproportionate impact on the estimates of the model parameters. Therefore, logistic regression assumes that there are no outliers in the data.

Boxplot of the predictor variables and look for any extreme values For Outliers

5.5 Large sample size

Logistic regression assumes that the sample size is large enough to estimate the model parameters accurately. As a rule of thumb, the sample size should be at least ten times the number of predictor variables.

Python Code To Check Large Sample Size

To check for a large enough sample size, you can calculate the ratio of the number of observations to the number of predictor variables. As a rule of thumb, this ratio should be at least 10:1.

5.6 Binary outcome:

Logistic regression is designed for binary outcomes, where the response variable takes on one of two possible values. If the outcome variable is continuous, then linear regression is a more appropriate model.

Python Code To Check Binary Outcome

To check for a binary outcome, you can calculate the unique values of the outcome variable. If there are more than two unique values, then logistic regression may not be the appropriate model.

5.7 No perfect separation:

Logistic regression assumes that there is no perfect separation in the data, which means that there is no combination of predictor variables that can perfectly predict the outcome variable.

Python Code To Check No perfect Separation

To check for perfect separation, you can fit the logistic regression model and check for warnings or errors related to convergence. If the model fails to converge, it may be due to perfect separation in the data.

6. Applications of the Logistic Regression:

Logistic regression is a popular statistical method that is commonly used in various fields, including finance, healthcare, marketing, social sciences, and more. Here are some of the most common applications of logistic regression:

6.1 Binary Classification:

Logistic regression is most commonly used for binary classification problems, where the goal is to predict whether an observation belongs to one of two classes. For example, predicting whether a customer will churn or not, or whether a patient has a particular disease or not.

6.2 Customer Segmentation:

Logistic regression can be used to segment customers based on their characteristics or behavior. This helps businesses to tailor their marketing strategies to different groups of customers.

6.3 Credit Scoring:

Logistic regression is often used in credit scoring models to predict the probability of default on a loan or credit card. This helps lenders to make better decisions about which applicants to approve and at what interest rates.

6.4 Fraud Detection:

Logistic regression can be used to detect fraudulent transactions or activities. By analyzing patterns of behavior, logistic regression models can flag suspicious transactions and alert fraud investigators.

6.5 Market Analysis:

Logistic regression can be used to analyze consumer behavior and predict market trends. By identifying factors that influence buying decisions, businesses can adjust their marketing strategies to better target their customers.

6.6 Medical Diagnosis:

Logistic regression can be used to diagnose diseases or medical conditions based on patient symptoms or test results. This helps doctors to make better treatment decisions and improve patient outcomes.

6.7 Political Science:

Logistic regression can be used to model voting patterns or predict election outcomes. By analyzing polling data and demographic trends, logistic regression models can provide insights into political behavior.

6.8 Sports Analytics:

Logistic regression can be used to predict the outcome of sporting events based on team performance and other factors. This helps sports teams to make better decisions about player recruitment and game strategies.

7. Challenges and limitations of logistic Regression

Like any statistical method, logistic regression has certain challenges and limitations that should be taken into account when using it. Here are some of the challenges and limitations of logistic regression:

7.1 Linearity Assumption:

Logistic regression assumes that the relationship between the independent variables and the log odds of the dependent variable is linear. If this assumption is not met, the model may produce biased or inaccurate results.

7.2 Overfitting:

Logistic regression models can overfit the data, meaning they fit the noise rather than the underlying signal. This can lead to poor performance on new, unseen data.

7.3 Multicollinearity:

If the independent variables in the model are highly correlated with each other, this can lead to unstable estimates and difficulty interpreting the results.

7.4 Outliers:

Logistic regression models can be sensitive to outliers, which can distort the estimates and lead to inaccurate predictions.’

7.5 Imbalanced Classes:

When the classes in the dependent variable are imbalanced, meaning one class is much more prevalent than the other, logistic regression can produce biased results. This can be mitigated by using techniques such as undersampling or oversampling.

7.6 Model Interpretation:

Logistic regression can be challenging to interpret, particularly when there are multiple independent variables in the model. It can be difficult to determine which variables are most important in predicting the outcome.

7.7 Limited Predictive Power:

Logistic regression is a linear model and may not capture complex nonlinear relationships between the independent variables and the dependent variable. In such cases, other machine learning algorithms such as decision trees or neural networks may be more appropriate.

7.8 Assumptions of Independence:

Logistic regression assumes that the observations are independent of each other, and violations of this assumption can lead to inaccurate estimates and standard errors.

7.9 Missing Data:

Logistic regression assumes that the data are complete, and missing data can be a challenge. Missing data can lead to biased estimates, and techniques such as imputation may be needed to handle missing data.

Overall, logistic regression is a powerful statistical tool, but its limitations and assumptions should be carefully considered when applying it to real-world problems.

Conclusion

In conclusion, logistic regression is a powerful statistical technique that allows us to model the probability of a binary event based on a set of input variables. It is widely used in machine learning and data analysis, and its interpretability makes it a valuable tool for understanding the relationship between input variables and output probabilities.

 Logistic Regression is one of the most widely used Artificial Intelligence algorithms in real-life Machine Learning problems — thanks to its simplicity, interpretability, and speed. In the next few minutes we’ll understand what’s behind the working of this algorithm. In this article, I will explain logistic regression with some data, python examples, and output. 1. What  Read More Machine Learning 

Leave a Reply

Your email address will not be published. Required fields are marked *