mojolearn.RidgeClassifier#
- class mojolearn.RidgeClassifier(alpha=1.0, *, fit_intercept=True, copy_X=True, max_iter=None, tol=0.0001, class_weight=None, solver='auto', positive=False, random_state=None)#
Classifier using ridge regression on -1/+1 targets.
Follows scikit-learn’s RidgeClassifier: each class becomes a -1/+1 target column (one column for a binary problem), fitted by ridge regression with a Cholesky solve. Only
solver='auto'or'cholesky'is accepted, andpositive=Trueraises ValueError.- Parameters:
alpha (float, default 1.0) – L2 regularization strength (>= 0).
fit_intercept (bool, default True) – Whether to fit an intercept.
copy_X (bool, default True) – Accepted for compatibility;
Xis never modified.max_iter (int or None, default None) – Accepted for compatibility; ignored by the direct solver.
tol (float, default 1e-4) – Accepted for compatibility; ignored by the direct solver.
class_weight (dict, ‘balanced’ or None, default None) – Per-class weights, multiplied into
sample_weight; ‘balanced’ weighs classes inversely to their frequency.solver ({‘auto’, ‘cholesky’}, default ‘auto’) – Both run the Cholesky solver.
positive (bool, default False) – Not supported: True raises ValueError.
random_state (int or None, default None) – Accepted for compatibility; ignored.
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 (seemojolearn.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.
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.RidgeClassifier(alpha=1.0).fit(X, y)
Methods
Signed distance of each sample to the separating hyperplane(s).
fit(X, y[, sample_weight])Fit the model.
get_params([deep])The numeric mode this estimator runs in.
predict(X)Predict class labels.
score(X, y)Mean accuracy of
predict(X)againsty.set_params(**params)Set constructor parameters.
The GPU backend this estimator runs on.
- fit(X, y, sample_weight=None)#
Fit the model.
- 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 (RidgeClassifier) – The fitted model.
- Raises:
ValueError – On an unsupported
solverorpositive=True, a negativealpha, a single class, invalid weights, or whenX'X + alpha Icannot be factored (rescaleXor raisealpha).
- 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
Xhas a different number of features than infit.
- 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 andclasses_[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)againsty.- 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.