SparseGPClassifier#

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

A description of a sparse Gaussian process classifier, for data with too many rows for a GPClassifier.

It takes the same inputs, target, kernel, mean function and schema as a GPClassifier, and fitting it returns the same kind of fitted model. The data is summarised by num_inducing points, so the model is an approximation: it is never chosen for you. It never changes: fitting it returns a separate fitted model.

Without a minibatch_size the fit looks at all the rows at once, with the optimiser a GPClassifier uses. With one, it works through the rows in random minibatches, which suits data too large to process at once. The inducing points start at the centres of a k-means clustering of the rescaled inputs and are tuned during the fit together with the other settings. The same data and settings always give the same model.

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.

minibatch_size#

How many rows each iteration of the fit looks at. Leave it unset to fit on all the rows at once. A whole number above zero fits on random minibatches of that size, one per iteration, for exactly the iterations given to fit. skgp does not judge whether such a fit converged: it issues no ConvergenceWarning, and to_json() gives the iterations it ran and its final objective, with no finished_properly. Zero or a negative number is refused.

num_inducing#

How many inducing points summarise the data, a whole number above zero. If it is more than the number of rows in the training data, every row is used. Leave it unset to let skgp choose.

random_state#

A whole number from 0 to 9,223,372,036,854,775,807 that seeds everything random in the fit (the k-means starting points and the order of the minibatches). The same data, settings and random_state give the same model; change it to see how much a fit depends on it.

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.