Docsity

Logistic Regression: A Comprehensive Guide to Classification Modeling - Prof. Cumani, Appunti di Machine learning

Toggle sidebar
bg1
Logistic Regression: A Powerful
Discriminative Model for Classification
Author
Marc'Antonio Lopez
AI & Data Analytics student at Polytechnic University of Turin
Logistic Regression is a fundamental and widely used algorithm categorized as a discriminative
approach for solving classification problems. Unlike generative models (which learn the data
distribution for each class, i.e., ), Logistic Regression directly models the class posterior
probability . Its primary goal is to estimate the probability of an input
x belonging to a
specific class
c , typically expressed as .
Binary Logistic Regression (for Two Classes)
For classification tasks involving exactly two classes (often denoted as and ), Binary Logistic
Regression makes a core assumption: it directly posits that the log-posterior ratio (also known as
log-odds or logit) is a linear function of the input features
x .
This relationship is expressed as:
Here:
is a vector of weights, where each element corresponds to a feature in
x .
is a bias term (or intercept).
represents a linear combination of the input features.
Key Characteristic: Linear Decision Boundaries
This assumption of a linear relationship for the log-odds inherently leads to linear decision
boundaries. This property is also observed in some generative models, such as Gaussian classifiers
with tied covariance matrices (which are equivalent to Linear Discriminant Analysis - LDA). A linear
decision boundary means the classifier separates classes using a straight line (in 2D feature space) or
a flat hyperplane (in higher dimensions).
P(X∣C)
P(C∣X)
P(C=c∣X=x)
h
0h
1
log =
P(C=h ∣x)
0
P(C=h ∣x)
1wx+
Tb
w
b
w x +
Tb
pf3
pf4
pf5
pf8
pf9
pfa

Anteprima parziale del testo

Scarica Logistic Regression: A Comprehensive Guide to Classification Modeling - Prof. Cumani e più Appunti in PDF di Machine learning solo su Docsity!

Logistic Regression: A Powerful

Discriminative Model for Classification

Author Marc'Antonio Lopez AI & Data Analytics student at Polytechnic University of Turin

Logistic Regression is a fundamental and widely used algorithm categorized as a discriminative approach for solving classification problems. Unlike generative models (which learn the data distribution for each class, i.e., ), Logistic Regression directly models the class posterior probability. Its primary goal is to estimate the probability of an input x belonging to a specific class (^) c , typically expressed as.

Binary Logistic Regression (for Two Classes)

For classification tasks involving exactly two classes (often denoted as and ), Binary Logistic Regression makes a core assumption: it directly posits that the log-posterior ratio (also known as log-odds or logit ) is a linear function of the input features x.

This relationship is expressed as:

Here:

is a vector of weights , where each element corresponds to a feature in (^) x. is a bias term (or intercept). represents a linear combination of the input features.

Key Characteristic: Linear Decision Boundaries This assumption of a linear relationship for the log-odds inherently leads to linear decision boundaries. This property is also observed in some generative models, such as Gaussian classifiers with tied covariance matrices (which are equivalent to Linear Discriminant Analysis - LDA). A linear decision boundary means the classifier separates classes using a straight line (in 2D feature space) or a flat hyperplane (in higher dimensions).

P ( X ∣ C )

P ( C ∣ X )

P ( C = c ∣ X = x )

h 0 h 1

log =

P ( C = h 0 ∣ x )

P ( C = h 1 ∣ x ) w xT + b

w

b

w xT^ + b

Deriving Posterior Probability using the Sigmoid Function: From the linear log-odds relationship, the posterior probability of belonging to class given input and parameters can be derived using the sigmoid function (also known as the logistic function) :

Consequently, the posterior probability for class is:

Function of the Sigmoid: The sigmoid function is crucial because it transforms any real-valued output from the linear combination (which can range from to ) into a probability value bounded between 0 and 1. A large positive value maps close to 1, while a large negative value maps close to

Training Objective: The training process of Logistic Regression primarily aims to find the optimal values for the parameters that best fit the training data.

Parameter Estimation (Training the Logistic Regression Model)

Training a Logistic Regression model involves finding the optimal parameters that best explain the observed training data. This is typically achieved using Maximum Likelihood Estimation (MLE). The training data is assumed to be labeled, independent, and identically distributed (i.i.d.).

The objective function to maximize during training is the conditional log-likelihood :

This function measures how probable the observed true class labels are, given the input features and the current model parameters.

Equivalence to Minimizing Cross-Entropy Loss: Maximizing this conditional log-likelihood is mathematically equivalent to minimizing a commonly used loss function called the binary cross-entropy loss (also known as log loss):

h 1 x

( w , b )

σ ( z )

σ ( z ) = 1 + e − z

h 1

P ( C = h 1 ∣ x , w , b ) = σ ( w xT^ + b )

w xT^ + b −∞ +∞

w xT^ + b

( w , b )

( w , b )

ℓ( w , b ) = log P ( C =

i =

n

i c^ i ∣ X^ i = x^ i ,^ w ,^ b )

ci xi

( w , b )

solution), L2 regularization is commonly added to the objective function. L2 regularization adds a penalty term proportional to the squared Euclidean norm of the weight vector ; the bias term is typically not regularized.

The new objective function to minimize (combining empirical risk with regularization) becomes:

Here:

represents the individual logistic loss for each sample. is the squared L2 norm of the weight vector. (lambda) is a crucial hyper-parameter (set before training, not learned from data).

Role of : The hyper-parameter controls the trade-off between:

Fitting the training data well: A smaller allows the model to fit the training data more closely. Keeping the model simple (preventing large weights): A larger penalizes large weights more heavily, forcing them towards zero and creating a simpler model.

Consequences of Choice:

A high can lead to underfitting , where the model is too constrained and cannot effectively capture data patterns. A low (or , meaning no regularization) risks overfitting , especially with linearly separable data or noisy features.

Benefits of L2 Regularization:

Makes the objective function strictly convex , ensuring a unique, finite minimum for parameters , even with linearly separable data. Improves the model's generalization ability by preventing excessively large weights that are too sensitive to small fluctuations in training data.

Optimal Selection: The optimal value for is typically chosen through cross-validation , where different values are tried, and the one yielding the best performance on a validation set is selected.

Feature Preprocessing Recommendation: It is highly recommended to preprocess features by centering them (subtracting the mean) and

w b

R ( w , b ) = +

Empirical Risk

l ( z ( w x + b ))

n

i =

n i T^ i L2 Regularization

∥ w ∥

l (⋅)

∥ w ∥ 2 =∑ j wj^2

( w , b )

scaling them (dividing by standard deviation). This prevents features with naturally large scales from disproportionately dominating the regularization term, ensuring the regularization penalty is applied fairly across all features.

Interpreting Scores and Handling Class Priors

The linear output of the Logistic Regression model, , represents the log-posterior ratio (or log-odds) of class versus. This score inherently incorporates the empirical class priors (the observed class proportions) present in the training data.

Problem: Prior Mismatch: If the true application priors (the actual class prevalence in the real-world deployment environment, denoted as ) differ significantly from the empirical priors in the training data ( , ), then directly using for decision-making will be suboptimal. The model's probabilities will be skewed based on the training set's composition rather than the real world.

Adjustment for Application Priors: To properly adjust the scores for the correct application priors, one can "remove" the training prior component from the raw score :

This adjusted score now represents a pure Log-Likelihood Ratio (LLR). Decisions can then be made by comparing this to an optimal Bayes threshold that incorporates the application priors (as discussed in the previous section on Bayes Decisions).

Alternative Prior-Weighting during Training: Alternatively, if the target application priors are known before training, a technique called prior- weighted logistic regression can be employed. This involves weighting samples in the training loss function according to the desired application priors, allowing the model to directly optimize for those specific priors.

Performance on MNIST Pairwise Digit Classification (Equal Error Rate - EER):

s ( x ) = w xT^ + b

h 1 h 0

π 1 app, π 0 app^ n 1 / n n 0 / n

P ( C = 1 ∣ x ) = σ ( s ( x ))

s ( x )

s llr ( x ) =( w xT^ + b ) − log (

n 0

n 1

s llr( x )

s llr( x )

Here:

and are the weight vector and bias term specifically for class k. The numerator is the "unnormalized probability" or "score" for class k. The denominator is a normalizing factor that ensures all predicted probabilities for an input x sum to 1 across all classes.

Training Objective: Training a Multiclass Logistic Regression model involves minimizing the multiclass cross-entropy loss (also known as the negative log-likelihood for multiclass classification):

Where:

represents the matrix of all weight vectors ( ) and represents the vector of all bias terms ( ). is a binary indicator: it is 1 if sample truly belongs to class , and 0 otherwise. This is often called "one-hot encoding." is the predicted probability for class for sample by the softmax function.

Regularization for Multiclass: The Multiclass Logistic Regression model is technically over-parameterized (meaning there are redundant ways to represent the same decision boundaries). To address this and improve generalization, L2 regularization is typically applied to the weight vectors (usually not to the bias terms):

Decision Boundary Shape: Similar to its binary counterpart, Multiclass Logistic Regression models inherently learn linear decision boundaries between any pair of classes, meaning the feature space is partitioned by a set of hyperplanes.

Multiclass MNIST (10 Digits) Error Rates:

wk bk

ew^ kx + b

T (^) k

∑ j^ K =1^ ew^ jx + b

T (^) j

K

J ( W , b ) = − z log y

i =

n

k =

K ik ik

W w 1 , … , wK b

b 1 , … , bK

zik i k

y ik = P ( C = k ∣ x i ) k i

∥ w ∥

k =

K k^2

Feature Type

Tied Gaussian Model Error RAW (Full Features)

PCA (50 Features) 8.8% 8.8% 8.9% 13.3% 12.6%

PCA (9 Features) 7.8% 7.8% 8.2% 12.9% 12.3%

PCA+LDA (Combined)

Observations from the Multiclass MNIST Results:

Overall Performance: Multiclass Logistic Regression generally outperforms the Tied Gaussian model (which is equivalent to LDA) for multiclass classification on MNIST. Crucial Role of Regularization: Similar to the binary case, regularization proves crucial. For RAW features, adding a small amount of L2 regularization (e.g., ) significantly reduces the error rate from 8.0% to 7.4% , effectively preventing overfitting and improving generalization. Dimensionality Reduction Impact: While dimensionality reduction techniques like PCA and PCA+LDA can be beneficial for reducing feature space and potentially computational costs, they do not always significantly outperform using RAW features when proper regularization is applied to the Logistic Regression model. This highlights the importance of balancing the inherent complexity of the feature set with appropriate model constraints.

Non-linear Classification via Feature Expansion: Going Beyond

Linear Boundaries

As discussed, standard Logistic Regression is intrinsically limited to learning linear decision boundaries , meaning it can only separate linearly separable classes in the original feature space. To achieve non-linear decision boundaries , a powerful and common technique called feature expansion (also known as basis expansion or feature transformation) can be employed.

Mechanism of Feature Expansion: Feature expansion involves transforming the original input feature vector x into an expanded, higher- dimensional feature vector, typically denoted as. This transformation explicitly creates new features that are non-linear combinations of the original features.

ϕ ( x )

MNIST Performance with Quadratic Feature Expansion (Applied to PCA Features):

Let's examine the impact of quadratic feature expansion on Logistic Regression performance for MNIST, applied to already PCA-reduced features.

MNIST – Average pairwise EER for LR with quadratic feature expansion

Feature Type (Original)

Tied Gaussian Model EER

PCA (Features: 50) 1.0% 1.0% 0.9% 1.5% 0.8%

MNIST – Multiclass error rates for LR with quadratic feature expansion

Feature Type (Original)

Tied Gaussian Model Error

PCA (Features: 50) 2.3% 1.9% 1.7% 3.1% 3.6%

Observations from Quadratic Feature Expansion Results:

Dramatic Performance Improvement: Implementing quadratic feature expansion (after initial PCA) dramatically improves Logistic Regression performance. For example, the multiclass error rate on MNIST drops significantly from 8.8% (for linear LR on PCA-50) to 1.7%. This clearly demonstrates the substantial benefit of allowing for non-linear decision boundaries when classifying the MNIST dataset. Competitiveness with Gaussian Models: With this feature expansion, Logistic Regression becomes highly competitive with, or even outperforms, standard Gaussian models (e.g., Full Covariance Gaussian models). Regularization Remains Critical: Consistent with other Logistic Regression setups, regularization (tuning ) remains absolutely critical for achieving optimal results with feature expansion. The very low error rates (e.g., 0.9% EER, 1.7% multiclass error) are achieved at intermediate values, highlighting the need to balance model complexity introduced by expansion with regularization.