# Visualizing Logistic Regression: A Geometric Perspective

- Canonical: https://33rdsquare.com/geometrical-approach-to-understand-logistic-regression/
- Published: 2024-09-03
- Author: Jordan Brown
- Categories: [Artificial Intelligence & Machine Learning & ChatGPT](https://33rdsquare.com/category/tech/ai/)

---

Logistic regression is one of the most widely used machine learning algorithms for binary classification problems. Despite its name, logistic regression is actually a classification model rather than a regression model. It works by learning a linear decision boundary to separate two classes and outputting the probability of a data point belonging to the positive class.

While the mathematical formulation of logistic regression may seem complex at first, having a good visual intuition of how the model works is key to understanding its core concepts. In this blog post, we‘ll take a deep dive into the geometric interpretation of logistic regression and explore how it learns to separate classes in feature space.

## A Quick Primer on Logistic Regression

Before we delve into the geometry behind logistic regression, let‘s do a quick recap of how the model works at a high level. Given a set of input features X and binary target variable y, logistic regression learns a set of weights w and a bias term b such that:

σ(w^T x + b) = p(y=1|x)

Here, σ represents the sigmoid function which "squashes" the output to a probability between 0 and 1. w^T x + b is a linear function of the input features. If this term is positive and large, the sigmoid will output a probability close to 1. If it‘s negative and large, the probability will be close to 0.

During training, logistic regression tunes the weights w to maximize the likelihood of the observed data. In practice, we minimize the negative log likelihood loss function:

L = -Σ [y log(p) + (1-y) log(1-p)]

By minimizing this loss using an optimization algorithm like gradient descent, logistic regression finds the weights that best fit the training data.

## The Geometry of Linear Decision Boundaries

At its core, the goal of logistic regression is to find a hyperplane decision boundary that best separates the positive and negative classes in feature space. This decision boundary is defined by the weights w and bias term b in the linear function w^T x + b.

Geometrically, the weights w determine the orientation or direction of the decision boundary hyperplane while the bias term b shifts the hyperplane towards one class or the other. Points lying on one side of the hyperplane will be classified as the positive class while points on the other side are classified as negative.

We can visualize this in a simple 2D case. Let‘s say we have two features x1 and x2. Logistic regression will learn values for w1, w2 and b to define a line that separates the two classes. The line is defined by the equation:

w1 _x1 + w2_ x2 + b = 0

All points lying above this line, where w1 _x1 + w2_ x2 + b > 0, will have a predicted probability > 0.5 and be classified as positive. Points below the line have probability < 0.5 and are classified as negative.

The distances of points from the decision boundary line determine how confident logistic regression is in its predictions. Points far from the line on the positive side will have predicted probabilities close to 1, while points on the negative side will be close to 0. The sigmoid function captures this, mapping the raw distance to a probability.

## Limitations of Linear Separability

One major limitation of logistic regression is that it can only learn linear decision boundaries. If the classes are not linearly separable, logistic regression will be unable to find a hyperplane that perfectly classifies the training data.

In these cases, logistic regression will still find the hyperplane that best separates the classes, but there will be some unavoidable classification errors. No matter how you orient the line, some points from one class will fall on the wrong side.

This linear separability limitation is why logistic regression tends to work best on problems where the classes have a linear relationship or can be made linearly separable via feature engineering. For highly nonlinear problems, more complex models like neural networks or kernel SVM are preferred.

## Regularization and Avoiding Overfitting

Like any machine learning model, logistic regression is prone to overfitting the training data, especially in high-dimensional feature spaces. Overfitting means the model learns an overly complex decision boundary to perfectly fit the training set but does not generalize well to new data.

Regularization techniques are used to constrain the complexity of the logistic regression model and combat overfitting. The two most common forms are L1 and L2 regularization, which add a penalty term to the loss function encouraging the model to keep weights small.

Geometrically, regularization has the effect of keeping the decision boundary hyperplane from becoming overly contorted or "wiggly" to account for every training data point. An overly complex boundary may fit the training set well but is less likely to generalize.

By keeping weights small, regularization helps keep the decision boundary smooth and aids generalization. The regularization hyperparameter lambda controls the strength of the penalty. Setting lambda too high leads to oversimplified boundaries and underfitting while setting it too low allows more complex boundaries and overfitting.

## Comparison to Other Linear Classifiers

Logistic regression belongs to a family of linear classification models along with others like Support Vector Machines (SVM) and Fisher‘s Linear Discriminant. All of these aim to find a linear decision boundary hyperplane, but differ in how they achieve it.

The standard "hard margin" SVM, for instance, finds the maximum margin hyperplane – the linear boundary that maximizes the distance to the nearest points of each class. This tends to result in a very robust fit if the classes are linearly separable.

Fisher‘s linear discriminant, on the other hand, finds the projection direction such that the classes are maximally separated after projection while points within each class are compact. This takes into account the variance structure of each class.

Compared to these, the probabilistic approach of logistic regression has some advantages. The sigmoid function allows it to output well-calibrated probabilities rather than just hard 0/1 classifications. It‘s also computationally efficient to train and less sensitive to outliers than SVMs.

## Implementing Logistic Regression in Python

Implementing logistic regression in Python is straightforward thanks to the scikit-learn library. Here‘s a simple example of training a logistic regression model on the iris dataset:

```
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

# Load iris dataset
X, y = load_iris(return_X_y=True)

# Train logistic regression model
model = LogisticRegression()
model.fit(X, y)

# Evaluate on training set
print(model.score(X, y))
```

We can also visualize the learned decision boundaries. Here‘s an example plotting the decision regions on the first two features of the iris dataset:

```
import matplotlib.pyplot as plt
import numpy as np

# Predict probability scores
y_scores = model.predict_proba(X)[:, 1]

# Create a grid of points
x0, x1 = np.meshgrid(
        np.linspace(X[:,0].min(), X[:,0].max(), 500),
        np.linspace(X[:,1].min(), X[:,1].max(), 500)
    )

# Predict at each grid point
Z = model.predict_proba(np.c_[x0.ravel(), x1.ravel()])[:,1]
Z = Z.reshape(x0.shape)

# Plot the contour and scatter
plt.contourf(x0, x1, Z, levels=20, cmap="RdBu")
plt.colorbar()
plt.scatter(X[:,0], X[:,1], c=y, cmap="coolwarm", edgecolors=‘k‘)

plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.title("Logistic Regression Decision Boundary")
plt.show()
```

This will produce a plot showing the learned logistic regression decision boundaries separating the classes in the 2D feature space.

## Applications and Use Cases

Logistic regression is a versatile classification model with many applications across different fields. Some common use cases include:

- Medical diagnosis: predicting presence of disease from symptoms and risk factors
- Customer churn: predicting if a customer will churn based on behavior
- Fraud detection: classifying transactions as fraudulent or legitimate
- Ad click prediction: predicting if a user will click on an ad
- Spam email filtering: classifying emails as spam or not spam

In general, logistic regression is a good baseline model to try on any binary classification problem, especially if the classes seem to have a linear relationship. Its simplicity, interpretability, and efficiency make it a popular choice.

## Strengths and Weaknesses

To summarize, some key strengths of logistic regression include:

- Output of well-calibrated probabilities
- Computationally efficient to train
- Highly interpretable weights and intuitive geometric formulation
- Robust to outliers compared to SVMs
- No distributional assumptions on features

Some weaknesses are:

- Can only learn linear decision boundaries
- Prone to overfitting in high dimensions without regularization
- Requires linearly separable classes for good performance
- Sensitive to multicollinearity in features

Understanding these tradeoffs is key when deciding if logistic regression is appropriate for a given problem. While not suited for every situation, logistic regression remains a foundational model in the machine learning practitioner‘s toolkit.

## Extensions and Variations

Since its introduction in the 1950s, logistic regression has seen many extensions and variations. Some more recent developments include:

- Bayesian logistic regression which takes a probabilistic view and incorporates prior beliefs about the weights
- Multinomial logistic regression that extends the binary model to multi-class problems
- Kernel logistic regression that can learn nonlinear boundaries via the kernel trick
- Sparse logistic regression using L1 regularization for feature selection
- Online logistic regression for streaming data and large datasets

Research into improving and building upon logistic regression remains active, cementing its status as a fundamental classification model.

## Conclusion

Logistic regression is a powerful yet simple classification model with an intuitive geometric interpretation. By learning a linear decision boundary to separate classes and outputting calibrated probabilities, logistic regression remains a go-to for many binary classification problems.

While limited to linearly separable problems, logistic regression‘s simplicity and efficiency make it an excellent baseline and often hard to beat. Understanding the geometric intuitions of logistic regression is key to knowing when it is likely to perform well and when a more complex, nonlinear model may be required.

I hope this deep dive into the geometry of logistic regression has been illuminating and provided some helpful intuitions. While mastery of the mathematical details is important, having a solid grasp of the geometric concepts makes reasoning about and applying logistic regression much more approachable. Armed with these insights, you should be well-equipped to tackle a variety of real-world classification problems.

---

Source: [Visualizing Logistic Regression: A Geometric Perspective](https://33rdsquare.com/geometrical-approach-to-understand-logistic-regression/)
