SparseGPRegressor#
- class SparseGPRegressor(features: str | Iterable[str], target: str, *, kernel: Literal['rbf', 'matern32', 'matern52'] | AbstractKernel = 'matern52', mean_function: Literal['zero', 'constant', 'linear'] | AbstractMeanFunction = 'constant', learned_noise: Literal['constant', 'heteroscedastic'] = '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 regression model, for data with too many rows for an exact one.
It takes the same inputs, target, kernel, mean function, learned noise and schema as a
GPRegressor, but no known noise, and fitting it returns the same kind of fitted model. The data is summarised bynum_inducingpoints, so the model is an approximation: it is never chosen for you. Noise is Gaussian. It never changes: fitting it returns a separate fitted model.Without a
minibatch_sizethe fit looks at all the rows at once. 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
dataand return it asCheckedData.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’snoise_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. Withfor_prediction=Trueonly 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. Yourschema, if you gave one, runs first; whatever it raises propagates. RaisesValueError, naming the column at fault, when one of skgp’s own checks fails.fitandpredictaccept 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 throughloguru, asfitandpredictdo: the settings given, which checks ran and whether they passed, and the names of any columns ignored. With the defaultFalseit emits nothing.
- fit(data: NativeDataFrame | CheckedData, *, iterations: int = 2000, verbose: bool = False) FittedGPRegressor#
Fit to
data, returning a separate fitted model.datais a Polars, pandas or other dataframe, which is checked first, or aCheckedDatafrom thecheckof this model or of any regressor with the same features, target, schema and column of known noise, which is not checked again.selfis unchanged.iterationsis a whole number above zero. For aGPRegressor, or aSparseGPRegressorwithout aminibatch_size, it is the most iterations the optimiser may take; it stops sooner once the fit has finished. For aSparseGPRegressorwith aminibatch_size, it is exactly the number of iterations run, each on one minibatch.With
verbose=True, skgp emits log records throughloguru, each a short message with named fields (inrecord["extra"]), so you choose where they go. A long fit reports its progress every few seconds, except aSparseGPRegressorwith aminibatch_size, which shows GPJax’s progress bar on standard error instead. With the defaultFalseit emits nothing.If a fit on all rows at once does not finish properly (the same condition
to_json()reports asfinished_properly: false),fitissues oneskgp.ConvergenceWarning, aUserWarning, whateververboseis, and still returns the fitted model, which may be less reliable. Filter or escalate it with thewarningsmodule. The warning says what to check: the scale of your data and whether it is noise-free, and for a fit that used all its iterations without settling, a largeriterations. Fits that finish properly are silent. ASparseGPRegressorwith aminibatch_sizeruns exactly itsiterations, 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
changesmade, for examplemodel.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 withparamax.non_trainablestays where you put it. A kernel with one length scale keeps one; give one per input, such aslengthscale=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’sPeriodickernel 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’sactive_dimsare positions infeatures, counting from 0. Any other value, andactive_dimspast the last feature, are refused when the model is defined.
- learned_noise: Literal['constant', 'heteroscedastic']#
The noise on each observation that the model learns from the data, and how it varies:
"constant"(the default), the same at every row, or"heteroscedastic", varying with the inputs (seeGPRegressor’slearned_noise). ASparseGPRegressortakes no known noise, so it always learns its noise.
- 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 withparamax.non_trainablestays 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
iterationsgiven tofit. skgp does not judge whether such a fit converged: it issues noConvergenceWarning, andto_json()gives the iterations it ran and its final objective, with nofinished_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_stategive 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 atfitandpredict), 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
validatereturns 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
CheckedDatapassed tofitorpredictis not validated again.
A value with no callable
validateis rejected when the model is defined. The schema is not included in a fitted model’sto_json(). Two models are equal only if their schemas are equal.
- settings_not_taken: ClassVar[dict[str, str]] = {'noise_column': "GPJax's sparse fits take the same noise at every row. Use a GPRegressor for known noise."}#
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.