Skip to main content

LogisticRegression

#include <Skigen/LinearModel>

template <typename Scalar = double>
class Skigen::LogisticRegression(C=1, fit_intercept=true, max_iter=100, tol=1e-4)

Logistic Regression (aka logit, MaxEnt) classifier.

In the multiclass case, the training algorithm uses a one-vs-rest (OvR) scheme. Each binary sub-problem is solved with an Iteratively Reweighted Least Squares (IRLS / Newton) solver that minimizes:

minw  1ni=1nlog ⁣(1+eyi(Xiw+b))+12Cw22\min_w \;\frac{1}{n}\sum_{i=1}^n \log\!\bigl(1 + e^{-y_i (X_i w + b)}\bigr) + \frac{1}{2C}\|w\|_2^2

Mirrors sklearn.linear_model.LogisticRegression.


Parameters:

  • C : Scalar, default=1 Inverse of regularization strength (Scalar, default 1). Must be positive. Smaller values specify stronger regularization.

  • fit_intercept : bool, default=true Whether the intercept should be estimated (bool, default true).

  • max_iter : int, default=100 Maximum number of IRLS iterations (int, default 100).

  • tol : Scalar, default=1e-4 Tolerance for stopping criteria (Scalar, default 1e-4).


Attributes:

  • coef : MatrixType Coefficient matrix (n_classes × n_features or 1 × n_features for binary).

  • intercept : VectorType Intercept (bias) vector of shape (n_classes,) or (1,).

  • classes : const std::vector< int > & Unique class labels sorted in ascending order.


Methods

fit(X, y)

Fit the model according to the given training data.

Discovers unique classes in y, then solves binary logistic regression sub-problems (OvR for multiclass) via IRLS.

Parameters:

  • X : MatrixType Training matrix of shape (n_samples, n_features).

  • y : IndexVector Target vector of shape (n_samples,) with integer class labels.

Returns:

  • result : LogisticRegression Reference to the fitted estimator (*this).

Throws:

  • std::invalid_argument — if fewer than 2 classes are found or X and y have inconsistent lengths.
note

sklearn parity gap: sample_weight, class_weight parameters are not yet supported.


predict(X)

Predict class labels for samples in X.


predict_proba(X)

Probability estimates for each class.

Returns a matrix of shape (n_samples, n_classes) where each row sums to 1. For binary, column 0 is the probability of class 0, column 1 of class 1. For multiclass, probabilities are normalized per-row.

Parameters:

  • X : MatrixType Sample matrix of shape (n_samples, n_features).

Returns:

  • result : MatrixType Probability matrix (n_samples, n_classes).

Throws:

  • std::runtime_error — if the model has not been fitted.

Example

// Train logistic regression
Skigen::LogisticRegression<double> model(/*C=*/1.0);
model.fit(split.X_train, split.y_train);

auto y_pred = model.predict(split.X_test);

std::cout << "=== Logistic Regression ===\n";
std::cout << "Accuracy: " << Skigen::Metrics::accuracy_score(split.y_test, y_pred) << "\n";
std::cout << "Precision: " << Skigen::Metrics::precision_score(split.y_test, y_pred) << "\n";
std::cout << "Recall: " << Skigen::Metrics::recall_score(split.y_test, y_pred) << "\n";
std::cout << "F1 Score: " << Skigen::Metrics::f1_score(split.y_test, y_pred) << "\n\n";