Skip to main content
Regression algorithms predict continuous or categorical output values based on input features.

LogisticRegression - Logistic Regression Classifier

Logistic Regression is a statistical method for binary and multi-class classification problems. Despite its name, it’s primarily used for classification rather than regression.

Creating a Logistic Regression Model

Key Methods

setLearningRate
void
Sets the learning rate for gradient descentParameters:
  • val (double): Learning rate (step size for parameter updates)
Typical values: 0.001 to 0.1. Higher values train faster but may overshoot the optimal solution.
setIterations
void
Sets the number of training iterationsParameters:
  • val (int): Maximum number of iterations
More iterations can lead to better convergence but increase training time.
setRegularization
void
Sets the kind of regularization to be appliedParameters:
  • val (int): Regularization type
Types:
  • LogisticRegression::REG_DISABLE (-1): No regularization
  • LogisticRegression::REG_L1 (0): L1 norm regularization (promotes sparsity)
  • LogisticRegression::REG_L2 (1): L2 norm regularization (prevents overfitting)
setTrainMethod
void
Sets the training method usedParameters:
  • val (int): Training method
Methods:
  • LogisticRegression::BATCH (0): Batch gradient descent
  • LogisticRegression::MINI_BATCH (1): Mini-batch gradient descent
setMiniBatchSize
void
Sets the number of training samples taken in each Mini-Batch Gradient Descent stepParameters:
  • val (int): Mini-batch size (must be less than total training samples)
Only used when training method is MINI_BATCH. Smaller batches provide more frequent updates but noisier gradients.
setTermCriteria
void
Sets the termination criteria of the algorithmParameters:
  • val (TermCriteria): Criteria for stopping training
Example: TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, 0.001)
train
bool
Trains the logistic regression modelParameters:
  • samples (InputArray): Training samples (CV_32F type)
  • layout (int): Sample layout (ROW_SAMPLE or COL_SAMPLE)
  • responses (InputArray): Training labels/responses
Returns: bool - true if training succeeded
predict
float
Predicts responses for input samplesParameters:
  • samples (InputArray): Input data for prediction (CV_32F type, m × n matrix)
  • results (OutputArray): Predicted labels as column matrix (CV_32S type)
  • flags (int): Optional flags (not used)
Returns: float - predicted value for single sample
get_learnt_thetas
Mat
Returns the trained parametersReturns: Mat - learnt parameters of Logistic Regression (CV_32F type)For two-class classification, returns a row matrix. These are the weights/coefficients learned during training.

Example Usage

Multi-Class Classification

For multi-class problems (more than 2 classes), Logistic Regression uses a one-vs-rest approach:
For binary classification, ensure your labels are 0 and 1. For multi-class classification, use consecutive integers starting from 0 (e.g., 0, 1, 2, 3…).

Linear Regression with Normal Equations

While OpenCV doesn’t have a dedicated linear regression class, you can perform linear regression using the solve() function with normal equations or use the SVM class with SVM::EPS_SVR type.

Method 1: Using Normal Equations

Linear regression can be solved directly using the normal equation: θ = (X^T X)^(-1) X^T y

Method 2: Using SVM for Regression

For more robust regression with regularization, use SVM::EPS_SVR:

Method 3: Using Decision Trees for Regression

DTrees can also be used for regression by setting appropriate parameters:

Polynomial Regression

For polynomial regression, transform your input features to include polynomial terms:
When using polynomial features, consider normalizing your data first to prevent numerical instability. Higher degree polynomials can lead to overfitting.

Regularized Regression

For Ridge Regression (L2 regularization), modify the normal equation: θ = (X^T X + λI)^(-1) X^T y

Best Practices

Feature Scaling

Always normalize features when using gradient-based methods:

Cross-Validation

Use cross-validation to evaluate model performance:

Hyperparameter Tuning

Try different learning rates and regularization parameters:

See Also