mojolearn.PassiveAggressiveClassifier#
- class mojolearn.PassiveAggressiveClassifier(*, C=1.0, fit_intercept=True, max_iter=1000, tol=0.001, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, shuffle=True, verbose=0, loss='hinge', n_jobs=None, random_state=None, warm_start=False, class_weight=None, average=False, batch_size=256)#
Passive-aggressive classifier (PA-I or PA-II).
Follows scikit-learn’s PassiveAggressiveClassifier, one-vs-rest for more than two classes:
loss='hinge'is PA-I and'squared_hinge'is PA-II. Unlike scikit-learn, the default fit runs minibatches ofbatch_size=256rows;batch_size=0gives scikit-learn’s per-sample updates.- Parameters:
C (float, default 1.0) – Maximum step size (> 0).
fit_intercept (bool, default True) – Whether to fit an intercept.
max_iter (int, default 1000) – Maximum number of passes over the data.
tol (float or None, default 1e-3) – Stopping tolerance, as in scikit-learn; None disables it.
early_stopping (bool, default False) – Not supported: True raises ValueError.
validation_fraction (float, default 0.1) – Accepted for compatibility; ignored (early stopping is not supported).
n_iter_no_change (int, default 5) – Passes without improvement before stopping.
shuffle (bool, default True) – Whether to shuffle the rows each pass.
verbose (int, default 0) – Accepted for compatibility; ignored.
loss ({‘hinge’, ‘squared_hinge’}, default ‘hinge’) – PA-I (‘hinge’) or PA-II (‘squared_hinge’).
n_jobs (int or None, default None) – Accepted for compatibility; ignored.
random_state (int or None, default None) – Non-negative seed for shuffling. None means seed 0.
warm_start (bool, default False) – Not supported: True raises ValueError.
class_weight (dict, ‘balanced’ or None, default None) – Per-class weights; ‘balanced’ weighs classes inversely to their frequency.
average (bool, default False) – Not supported: True raises ValueError.
batch_size (int, default 256) – Rows per minibatch step; 0 runs per-sample updates.
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_iter_ (int) – Number of passes run.
t_ (float) – Number of weight updates,
n_iter_ * n_samples + 1.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.PassiveAggressiveClassifier().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 (PassiveAggressiveClassifier) – The fitted model.
- Raises:
ValueError – On an unsupported option or loss,
C <= 0, a single class, invalid weights, or a floating-point overflow during training.
- 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.