mojolearn.LogisticRegressionCV#

class mojolearn.LogisticRegressionCV(*, Cs=10, fit_intercept=True, cv=None, dual=False, penalty='l2', scoring=None, solver='lbfgs', tol=0.0001, max_iter=100, class_weight=None, n_jobs=None, verbose=0, refit=True, intercept_scaling=1.0, random_state=None, l1_ratios=None)#

Logistic regression with C chosen by stratified K-fold cross-validation.

Follows scikit-learn’s LogisticRegressionCV with an L2 penalty, binary or multinomial, solved by L-BFGS. Each candidate C is scored by accuracy on stratified folds, and the model is refit on all rows with the best one. Only penalty='l2' (primal), scoring=None and refit=True are supported; other values raise ValueError.

Parameters:
  • Cs (int or sequence of float, default 10) – Candidate inverse regularization strengths; an int m tries m values log-spaced between 1e-4 and 1e4.

  • fit_intercept (bool, default True) – Whether to fit an intercept.

  • cv (int or None, default None) – Number of stratified folds, from 2 to the size of the largest class; None means 5.

  • dual (bool, default False) – Only False is supported.

  • penalty ({‘l2’}, default ‘l2’) – Only ‘l2’ is supported.

  • scoring (None, default None) – Only None (accuracy) is supported.

  • solver ({‘lbfgs’, ‘newton-cg’, ‘newton-cholesky’}, default ‘lbfgs’) – All three run L-BFGS.

  • tol (float, default 1e-4) – L-BFGS stopping tolerance.

  • max_iter (int, default 100) – Maximum number of L-BFGS iterations per fit.

  • class_weight (dict, ‘balanced’ or None, default None) – Per-class weights; ‘balanced’ weighs classes inversely to their frequency.

  • n_jobs (int or None, default None) – Accepted for compatibility; ignored.

  • verbose (int, default 0) – Accepted for compatibility; ignored.

  • refit (bool, default True) – Only True is supported.

  • intercept_scaling (float, default 1.0) – Accepted for compatibility; ignored.

  • random_state (int or None, default None) – Accepted for compatibility; ignored.

  • l1_ratios (None, default None) – Only None is supported.

  • numeric_mode (str or None, default None) – Numeric mode for this model ('identical' or 'fast', which runs on Apple GPUs only); None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • classes_ (array of shape (n_classes,)) – Class labels.

  • coef_ (float32 array of shape (1, n_features) or (n_classes, n_features)) – Weights; one row for a binary problem.

  • intercept_ (float32 array of shape (1,) or (n_classes,)) – Intercepts.

  • Cs_ (list of float) – Candidate C values.

  • C_ (list of float) – Selected C, repeated once per row of coef_.

  • scores_ (dict) – For each class label (the positive class only, for a binary problem), a float32 array of shape (n_folds, n_Cs) of fold scores.

  • n_iter_ (int) – L-BFGS iterations of the refit.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] > X[:, 1]).astype(np.int32)
>>> model = mojolearn.LogisticRegressionCV(Cs=5).fit(X, y)
>>> proba = model.predict_proba(X)

Methods

decision_function(X)

Signed distance of each sample to the separating hyperplane(s).

fit(X, y[, sample_weight])

Fit the model and select C.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels.

predict_proba(X)

Class probabilities.

score(X, y)

Mean accuracy of predict(X) against y.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)#

Fit the model and select C.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32; values must be finite.

  • y (array-like of shape (n_samples,)) – Class labels; at least two classes.

  • sample_weight (array-like of shape (n_samples,) or float, optional) – Non-negative weights with a positive sum.

Returns:

self (LogisticRegressionCV) – The fitted model.

Raises:

ValueError – On an unsupported option, a single class, invalid weights, or a cv outside [2, the size of the largest class].

predict_proba(X)#

Class probabilities.

The sigmoid of the score for a binary problem, the softmax of the class scores otherwise.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

proba (float32 array of shape (n_samples, n_classes)) – Probability of each class, in the order of classes_.

decision_function(X)#

Signed distance of each sample to the separating hyperplane(s).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32. The feature count must match the one seen in fit.

Returns:

scores (float32 array of shape (n_samples,) or (n_samples, n_classes)) – X @ coef_.T + intercept_. One column (returned as a 1-D array) for a binary problem, one column per class otherwise.

Raises:
  • RuntimeError – If the model is not fitted.

  • ValueError – If X has a different number of features than in fit.

numeric_mode_used()#

The numeric mode this estimator runs in.

Returns:

str – 'identical' or 'fast', read from the compiled binary the estimator uses, not from the argument it was given.

predict(X)#

Predict class labels.

A binary model predicts classes_[1] where the decision score is positive and classes_[0] otherwise; a multiclass model predicts the class with the largest score (the first one on a tie).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

labels (array of shape (n_samples,)) – Predicted labels, drawn from classes_.

score(X, y)#

Mean accuracy of predict(X) against y.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,)) – True labels.

Returns:

score (float) – Fraction of samples predicted correctly.

set_params(**params)#

Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.

vendor_used()#

The GPU backend this estimator runs on.

Returns:

str – 'metal', 'cuda' or 'hip', read from the compiled binary the estimator uses.