Docs · Machine learning

logreg: logistic regression.

Binary logistic regression by full-batch gradient descent on the log-loss, the equivalent of Spark MLlib's LogisticRegression with a binomial family and no regularisation. It runs on the shared GLM engine.

Module
ml_logreg
Header
clio_ml/glm.h
Rows
features, then 0/1 label

What it does

Each iteration is one paged pass that sums the log-loss gradient over all rows, followed by one fixed-step update. No regularisation, no early stop. The recorded loss is the mean log-loss before each update. For more than two classes use multinomial logistic regression, which uses L-BFGS.

Rows

n_features feature columns followed by a 0/1 label. clio_ml.COLUMNS["logreg"] is 1.

Python

python
import numpy as np, clio_ml as cm
rng = np.random.default_rng(1)
X = rng.normal(size=(4096, 7)).astype(np.float32)
y = (X[:, 0] + X[:, 1] > 0).astype(np.float32)
data = np.column_stack([X, y]).astype(np.float32)

r = cm.fit_logreg(data, iters=20, learning_rate=0.1)
p = cm.predict("logreg", r, data[0, :7])       # P(y = 1) for one row
print(r.weights, p)
  • fit_logreg(X, **options) calls the GLM engine with GlmKind.logistic.
  • Options (GlmOptions): iters (100), learning_rate (0.1), threads (256).
  • Result GlmResult: weights (coefficients, then intercept), loss_history, stats.
  • predict("logreg", r, x) returns the probability of class 1.

C++

cpp
GlmResult FitLogisticRegression(Dataset &ds, const GlmOptions &opt);   // FitGlm(kLogistic, ...)
GlmResult FitGlm(GlmKind kind, Dataset &ds, const GlmOptions &opt);
GlmResult FitGlmPlain(GlmKind kind, const float *data, u64 n_rows, u32 n_features,
                      u64 elems_per_page, u32 nblocks, const GlmOptions &opt);
float GlmPredict(GlmKind kind, const std::vector<float> &w, const float *x);  // P(y=1)

GlmOptions and GlmResult are described on the linear regression page. clio_ml/logistic_regression.h only includes glm.h. The ChiMod is module ml_logreg.

Related: linear SVM, multinomial logistic regression, gradient-boosted classifier.