mojolearn.GaussianProcessClassifier#
- class mojolearn.GaussianProcessClassifier(kernel=None, *, optimizer=None, n_restarts_optimizer=0, max_iter_predict=100, warm_start=False, copy_X_train=True, random_state=None, multi_class='one_vs_rest', n_jobs=None)[source]#
Bases:
NumericModeMixinGaussian process classification by the Laplace approximation.
Follows scikit-learn’s
GaussianProcessClassifier: a latent Gaussian process with a logistic likelihood, fitted by Newton iteration (Rasmussen and Williams, Algorithms 3.1 and 3.2). Two classes give one binary model; more classes give one binary model per class against the rest, with probabilities normalized per row as in scikit-learn’s one-vs-rest wrapper. X is dense float32 in C order (other inputs are converted).Differences from scikit-learn that change results:
There is no hyperparameter optimizer.
optimizermust be None (the default here; scikit-learn’s is"fmin_l_bfgs_b"), sokernel_is the kernel you pass.Every fit starts from zero latent values;
warm_start=Trueis refused.
- Parameters:
kernel (Kernel or None, default None) – A combination of
RBF,Matern,ConstantKernelandWhiteKernelwith+and*. None meansConstantKernel(1.0) * RBF(1.0). Other kernel types raiseTypeError.optimizer (None, default None) – Only None (or
"none") is accepted; anything else raisesNotImplementedError.n_restarts_optimizer (int, default 0) – Only 0 is accepted (
NotImplementedErrorotherwise).max_iter_predict (int, default 100) – Maximum number of Newton iterations per binary fit; the iteration can stop earlier on convergence. Must be an int (
TypeError) of at least 1 (ValueError).warm_start (bool, default False) – Only False is accepted (
NotImplementedErrorotherwise).copy_X_train (bool, default True) – Only True is accepted: X is always copied to float32 memory.
random_state (None, default None) – Only None is accepted; without an optimizer nothing is random.
multi_class (“one_vs_rest”, default “one_vs_rest”) –
"one_vs_one"raisesNotImplementedError; any other value raisesValueError.n_jobs (None, default None) – Only None is accepted; the binary fits run on one device.
numeric_mode (str or None, default None) – The numeric mode for this estimator; None uses the process default. See the numeric modes guide.
- Variables:
classes_ (list) – The class labels, in sorted order.
n_classes_ (int) – Number of classes.
X_train_ (Array of shape (n_train, n_features), float32) – The training inputs.
estimators_ (list) – The binary fits: one for two classes, one per class past two. Each has
y_train_,L_,pi_,W_sr_,log_marginal_likelihood_value_andn_iter_.W_sr_ (y_train_, L_, pi_,) – Two classes only: the 0/1 targets, the lower Cholesky factor of
I + W_sr K W_sr, the training probabilities and the square root of the likelihood Hessian of the single binary fit.log_marginal_likelihood_value_ (float) – The binary fit’s log marginal likelihood, or the mean over the binary fits past two classes.
n_iter_ (int or list of int) – Newton iterations run: an int for two classes, one int per class past two.
kernel_ (Kernel) – The kernel passed in.
n_features_in_ (int) – Number of features seen during fit.
input_copied_ (bool) – Whether fit had to copy X to reach float32 C order.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.random((200, 4), dtype=np.float32) >>> y = (X[:, 0] + X[:, 1] > 1.0).astype(np.int32) >>> model = mojolearn.GaussianProcessClassifier(mojolearn.RBF(0.5)).fit(X, y) >>> proba = model.predict_proba(X[:5])
- __init__(kernel=None, *, optimizer=None, n_restarts_optimizer=0, max_iter_predict=100, warm_start=False, copy_X_train=True, random_state=None, multi_class='one_vs_rest', n_jobs=None)[source]#
Methods
fit(X, y)Fit the classifier.
get_params([deep])Mean and variance of the latent function at
X.load(path)Load a model written by
save.log_marginal_likelihood([theta, ...])Log marginal likelihood of the fitted model.
numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Predict class labels for
X.Class probabilities for
X.save(path)Save the fitted model to
pathas an.npzfile.score(X, y)Mean accuracy of
predictonXagainsty.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y)[source]#
Fit the classifier.
Encodes the classes, then runs one binary Laplace fit for two classes or one per class against the rest.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training inputs, converted to float32.
y (array-like of shape (n_samples,)) – Class labels; at least two distinct classes (
ValueErrorotherwise).
- Returns:
self (GaussianProcessClassifier)
- latent_mean_and_variance(X)[source]#
Mean and variance of the latent function at
X.Binary models only, as in scikit-learn; a model with more than two classes raises
ValueError.- Parameters:
X (array-like of shape (n_query, n_features)) – Query points, converted to float32.
- Returns:
mean (Array of shape (n_query,), float32)
var (Array of shape (n_query,), float32)
- predict_proba(X)[source]#
Class probabilities for
X.Past two classes, each row of the per-class probabilities is divided by its sum, as in scikit-learn’s one-vs-rest wrapper.
- Parameters:
X (array-like of shape (n_query, n_features)) – Query points, converted to float32.
- Returns:
Array of shape (n_query, n_classes), float64 – Columns follow
classes_.
- predict(X)[source]#
Predict class labels for
X.With two classes,
classes_[1]where the latent mean is positive andclasses_[0]elsewhere. Past two, the class with the largest one-vs-rest probability; a tie goes to the first such class.- Parameters:
X (array-like of shape (n_query, n_features)) – Query points, converted to float32.
- Returns:
labels (Array or list of shape (n_query,)) – Labels drawn from
classes_: an int64 or float64 Array for numeric classes, a list for string or bool classes.
- log_marginal_likelihood(theta=None, eval_gradient=False, clone_kernel=True)[source]#
Log marginal likelihood of the fitted model.
Only
theta=Noneis supported: it returnslog_marginal_likelihood_value_. A theta raisesNotImplementedErrorandeval_gradient=TrueraisesValueError.- Parameters:
theta (None, default None)
eval_gradient (bool, default False)
clone_kernel (bool, default True) – Accepted for scikit-learn compatibility; ignored.
- Returns:
float
- score(X, y)[source]#
Mean accuracy of
predictonXagainsty.- Parameters:
X (array-like of shape (n_samples, n_features)) – Test inputs.
y (array-like of shape (n_samples,)) – True labels.
- Returns:
float
- save(path)[source]#
Save the fitted model to
pathas an.npzfile.The file holds everything prediction reads, byte for byte: the kernel, the float32 training inputs, the classes, the numeric mode and each binary fit’s state. Raises
RuntimeErrorif the model is not fitted.- Parameters:
path (str or path-like) – Destination file.
- classmethod load(path)[source]#
Load a model written by
save.The loaded model supports
predict,predict_probaandlatent_mean_and_variance, including on a CPU-only install. Every array is read at its saved dtype and never cast; a file saved by a different estimator or with inconsistent contents raisesValueError.- Parameters:
path (str or path-like) – File written by
save.- Returns:
GaussianProcessClassifier