GPClassifier#

class GPClassifier(features: str | Iterable[str], target: str, *, kernel: Literal['rbf', 'matern32', 'matern52'] | AbstractKernel = 'matern52', mean_function: Literal['zero', 'constant', 'linear'] | AbstractMeanFunction = 'constant', schema: Any = None)[source]#

A description of a Gaussian process classifier, for a target with two classes.

It names the dataframe columns that are inputs, the column that is the target, the kernel, the mean function, and optionally a schema that checks your data first (see the schema setting). The target may hold any two different values: numbers, text, or true and false. The inputs and the target may be given by position: GPClassifier(["latitude", "altitude"], "rain"). A mistake in any setting raises ValueError when the model is defined. It never changes: fitting it returns a separate fitted model, and replace returns a new model with other settings.

The model is a smooth curve over the inputs, and the probability of the second class at a row is the probit (the standard normal distribution function) of the curve there. It is fitted by variational inference with an inducing point at every training row, so it gets slow and needs a lot of memory as rows grow, as an exact regressor does. When fit is given more rows than skgp.config.size_warning_rows (10,000 unless you change it), it issues one skgp.LargeDataWarning naming SparseGPClassifier, then fits as usual.

check(data: NativeDataFrame, *, for_prediction: bool = False, verbose: bool = False) → CheckedData#

Run every data check of this model on data and return it as CheckedData.

Training data (the default) needs the feature and target columns and at least 5 rows. Each input must hold numbers, or dates, or dates and times: a date input, which the model counts in days since 1970-01-01 in UTC (a date and time with no time zone is taken as UTC). A regressor’s target must hold numbers; a classifier’s must hold exactly two different values, of any type. A GPRegressor’s noise_column, if it has one, must hold numbers that are zero or more; rows to predict need it only for a prediction with noise, and it is checked whenever they have it. With for_prediction=True only the feature columns are needed, and any number of rows, and a target column is ignored. Any column that is neither an input, the target nor the column of known noise is ignored. Your schema, if you gave one, runs first; whatever it raises propagates. Raises ValueError, naming the column at fault, when one of skgp’s own checks fails.

fit and predict accept the result without checking it again, on this model or any other of the same task (regression or classification) with the same features, target, schema and column of known noise.

With verbose=True, skgp emits log records through loguru, as fit and predict do: the settings given, which checks ran and whether they passed, and the names of any columns ignored. With the default False it emits nothing.

fit(data: NativeDataFrame | CheckedData, *, iterations: int = 2000, verbose: bool = False) → FittedGPClassifier#

Fit to data, returning a separate fitted model.

data is a Polars, pandas or other dataframe, which is checked first, or a CheckedData from the check of this model or of any classifier with the same features, target and schema, which is not checked again. Its target column must hold exactly two different values. self is unchanged.

The two values are coded 0 and 1 in sorted order: the smaller number, false, or the text that comes first alphabetically is 0. iterations is a whole number above zero. For a GPClassifier, or a SparseGPClassifier without a minibatch_size, it is the most iterations the optimiser may take; it stops sooner once the fit has finished. For a SparseGPClassifier with a minibatch_size, it is exactly the number of iterations run, each on one minibatch.

With verbose=True, skgp emits log records through loguru, each a short message with named fields (in record["extra"]), so you choose where they go: among them, which of your values was coded 0 and which 1. A long fit reports its progress every few seconds, except a SparseGPClassifier with a minibatch_size, which shows GPJax’s progress bar on standard error instead. With the default False it emits nothing.

If a fit on all rows at once does not finish properly (the same condition to_json() reports as finished_properly: false), fit issues one skgp.ConvergenceWarning, a UserWarning, whatever verbose is, and still returns the fitted model, which may be less reliable. Filter or escalate it with the warnings module. If the fit used all its iterations, the warning suggests a larger iterations. Fits that finish properly are silent. A SparseGPClassifier with a minibatch_size runs exactly its iterations, and skgp does not judge whether it converged, so it never issues this warning.

replace(**changes: Any) → Self#

A new model like this one with changes made, for example model.replace(kernel="rbf"). This one is unchanged.

The new model is checked as any new model is, so a mistake raises ValueError. A setting you never gave keeps its default unless you change it.

features#

The columns of your dataframe that are the model’s inputs, in order: a list of column names, or a single name for one input. Each column is named once, and the target is not among them.

kernel#

The kind of curve the model expects: a name, or a GPJax kernel.

  • By name: "matern52" (the default) for a smooth curve, "matern32" for a rougher one, or "rbf" for a very smooth one. Each has one length scale per input, learned from the data, starting at that input’s standard deviation.

  • A GPJax kernel, such as gpjax.kernels.RBF() + gpjax.kernels.Periodic(period=52.0), is fitted as you give it. Its values are where the fit starts, and a value held with paramax.non_trainable stays where you put it. A kernel with one length scale keeps one; give one per input, such as lengthscale=jnp.array([1.0, 500.0]), for more.

Every kernel sees the inputs in the units of your columns, so a value that measures distance along an input, such as a length scale or a period, is in those units, both as you give it and as the fit reports it. A date input, an input column of dates or of dates and times, is in days since 1970-01-01, so gpjax.kernels.Periodic(period=365.25) repeats every year. (The length scale of GPJax’s Periodic kernel measures a fraction of the period instead, and has no units.) A regressor’s kernel variance is relative to the target’s variance, so 1 means the curve varies as much as the target does. A kernel’s active_dims are positions in features, counting from 0. Any other value, and active_dims past the last feature, are refused when the model is defined.

mean_function#

What the curve is far from the training data, where the data says nothing: a name, or a GPJax mean function. Near the data, the curve follows the data whatever the mean function is.

  • "constant" (the default): one number, learned from the data. A regressor’s starts at the target’s average.

  • "zero": zero in the target’s units, held there: for a target that goes back to 0 far from the data, such as a difference from a long-term average.

  • "linear": a number plus a slope along each input, all learned, so that a prediction far from the data keeps rising or falling along each input as the data does. A slope is in the target’s units per unit of its input’s column: per day along a date input. The slope is one value, learned, so a prediction’s spread does not include any doubt about it, which matters more the further from the data you predict.

  • A GPJax mean function, such as gpjax.mean_functions.Constant(15.0), sees the inputs in the units of your columns and gives the curve in the target’s units. Its values are where the fit starts, and a value held with paramax.non_trainable stays where you put it. On a fit on minibatches, each step moves a value by about a twentieth of the target’s standard deviation (of 1 for a classifier), or by about a twentieth of itself if it must be positive and is above 1, so start it near where it belongs: the names start at the target’s average.

With heteroscedastic noise, the mean function is the curve’s; the noise process keeps its own learned constant mean.

A classifier’s curve is the one it turns into probabilities through the probit, so its mean function has no units: far from the data, the probability of the second class goes towards the probit of the mean function there, nearer 0.5 the less the model knows. Any other value is refused when the model is defined.

schema = None#

Your own data checker: any object with a .validate(frame) method that raises on bad data. A pandera schema works; skgp does not depend on pandera.

It runs first, inside check (so at fit and predict), on the dataframe exactly as you passed it, in your own dataframe type, because a schema may fix types. skgp’s own checks then always run too and cannot be switched off.

  • If validate returns a dataframe (pandera returns the validated, possibly coerced, frame), skgp’s checks and the model use that returned frame.

  • If it returns None, skgp’s checks use the frame you passed.

  • Whatever it raises propagates unchanged and stops the call.

  • It also sees prediction rows, which have no target column, so it must accept that (in pandera, mark the target column required=False).

  • A CheckedData passed to fit or predict is not validated again.

A value with no callable validate is rejected when the model is defined. The schema is not included in a fitted model’s to_json(). Two models are equal only if their schemas are equal.

settings_not_taken = {}#

Settings another model takes and this one does not, each with the reason, which the refusal gives.

target#

The column of your dataframe that the model learns to predict.