mojolearn.Perceptron#

class mojolearn.Perceptron(*, penalty=None, alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, eta0=1.0, n_jobs=None, random_state=0, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, class_weight=None, warm_start=False, batch_size=256)[source]#

Bases: _LinearClassifierMixin, NumericModeMixin

Perceptron classifier.

Follows scikit-learn’s Perceptron: SGD with the perceptron loss and a constant step eta0, one-vs-rest for more than two classes. Unlike scikit-learn, the default fit runs minibatches of batch_size=256 rows, each step the sum of its rows’ updates; batch_size=0 gives scikit-learn’s per-sample updates.

Parameters:
  • penalty ({‘l2’, ‘l1’, ‘elasticnet’} or None, default None) – Regularization term.

  • alpha (float, default 0.0001) – Regularization strength, used with a penalty.

  • l1_ratio (float, default 0.15) – Elastic-net mixing; used with penalty='elasticnet'.

  • 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.

  • shuffle (bool, default True) – Whether to shuffle the rows each pass.

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

  • eta0 (float, default 1.0) – Constant step size (> 0).

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

  • random_state (int or None, default 0) – Non-negative seed for shuffling. None means seed 0.

  • 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.

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

  • warm_start (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', 'deterministic' 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.

  • 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.Perceptron().fit(X, y)
__init__(*, penalty=None, alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, eta0=1.0, n_jobs=None, random_state=0, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, class_weight=None, warm_start=False, batch_size=256)[source]#

Methods

decision_function(X)

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

fit(X, y[, sample_weight])

Fit the model.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels.

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)[source]#

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 (Perceptron) – The fitted model.

Raises:

ValueError – On an unsupported option, a single class, invalid weights, or a floating-point overflow during training (scale the inputs).