mojolearn.HuberRegressor#

class mojolearn.HuberRegressor(*, epsilon=1.35, max_iter=100, alpha=0.0001, warm_start=False, fit_intercept=True, tol=1e-05)#

Linear regression with the Huber loss, robust to outliers.

Follows scikit-learn’s HuberRegressor: an L2-regularized linear model whose scale is estimated jointly with the weights, solved by L-BFGS.

Parameters:
  • epsilon (float, default 1.35) – Residuals larger than epsilon * scale_ count as outliers; must be >= 1.0.

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

  • alpha (float, default 0.0001) – L2 regularization strength.

  • warm_start (bool, default False) – Not supported: True raises ValueError.

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

  • tol (float, default 1e-05) – Stopping tolerance.

  • 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:
  • coef_ (float32 array of shape (n_features,)) – Weights.

  • intercept_ (float) – Intercept.

  • scale_ (float) – Estimated scale of the residuals.

  • n_iter_ (int) – Number of iterations run.

  • outliers_ (list of bool) – For each training row, whether its absolute residual exceeds epsilon * scale_.

  • 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 @ np.arange(8, dtype=np.float32)
>>> model = mojolearn.HuberRegressor().fit(X, y)

See also

User guide: Linear models

Choosing an estimator

Methods

fit(X, y[, sample_weight])

Fit the model.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict targets for X.

score(X, y)

Coefficient of determination R^2 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.

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,)) – Targets.

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

Returns:

self (HuberRegressor) – The fitted model.

Raises:

ValueError – If epsilon < 1, warm_start is True or the weights are invalid.

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 targets for X.

Returns X @ coef_ + intercept_; models with a log link (Poisson, Gamma and Tweedie with power > 0) return its exponential.

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:

y_pred (float32 array of shape (n_samples,)) – Predicted values.

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

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

score(X, y)#

Coefficient of determination R^2 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 targets.

Returns:

score (float) – R^2; 1.0 is a perfect fit.

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.