FittedGPRegressor#

class FittedGPRegressor[source]#

A model fitted to data by a GPRegressor or a SparseGPRegressor. It never changes.

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

Run every data check on rows to predict and return them as CheckedData.

The same as check(data, for_prediction=True) on the model that was fitted, and also that each date input of the training data is a date input here, and every other input holds numbers: the feature columns are needed, the target is not, and any number of rows will do. Pass the result to predict, which does not check it again, apart from the date inputs. With verbose=True, skgp logs what the checks did.

predict(data: NativeDataFrame | CheckedData, *, include_noise: bool = True, suffix: str = '', batch_size: int = 1_000, verbose: bool = False) → Prediction[source]#

What the model expects at each row of data, as a Prediction.

data needs the feature columns but no target. It is a dataframe, which is checked first, or a CheckedData from the check of this model or of any regressor with the same features, target, schema and column of known noise (for training or prediction data), which is not checked again. Columns the model does not use are ignored, and a verbose log record names them. Every column of data comes back in Prediction.to_frame(), in the same kind of dataframe as data; a missing input column raises.

  • include_noise: True (the default) predicts a new observation, so the spread includes measurement noise: with heteroscedastic noise, the noise the model expects at that row; with known noise, each row’s own known noise, from the noise_column of data, which must then have it, plus any learned noise. False predicts the underlying curve without the noise: the same mean, with a smaller spread, and needs no noise_column.

  • suffix: text added to the names of the columns skgp adds (mean, std, lower and upper), for rows that already have a column with one of those names. With suffix="_predicted" they are mean_predicted and so on. Empty by default.

  • batch_size: how many rows are predicted at a time, a whole number above zero. Memory grows with it; the answers do not change.

The first prediction prepares the training data and keeps the result on the fitted model, so later predictions, and covariance() of any of them, reuse it. For a GPRegressor it takes memory that grows with the square of the training rows (about 72 MB at 3,000 rows and 800 MB at 10,000, twice as much with heteroscedastic noise).

With verbose=True, skgp emits log records through loguru, each a short message with named fields (in record["extra"]). With the default False it emits nothing.

score(data: NativeDataFrame | CheckedData, *, scoring: str | Callable[[...], float] | Iterable[str | Callable[[...], float]] | Mapping[str, str | Callable[[...], float]] = 'neg_crps') → Score[source]#

How well the model predicts the known targets of held-out data, as a Score: the value of each scorer, where higher is better.

data holds the feature columns and the target, a known number at every row, and, for a model with known noise, its noise_column. It is a dataframe, which is checked first, or a CheckedData of this model, which is not checked again but for the target. Other columns are ignored, as in predict.

scoring takes what cross_val_score(scoring=...) takes: a scikit-learn scorer’s name, such as "r2" or "neg_mean_absolute_error" (sklearn.metrics.get_scorer_names() lists them), a scorer itself, a list of either, or a dict from the name each value gets to its scorer. The default, "neg_crps", is skgp’s: the mean continuous ranked probability score (CRPS) with its sign flipped (see skgp.sklearn.neg_crps), in the target’s units. An unknown name raises scikit-learn’s ValueError.

The prediction scored is of new observations, noise included, because the targets are observations. A scorer that needs a point prediction, such as r2, gets the mean.

to_json() → str[source]#

A read-only JSON description of the fitted model, for inspection and audit.

It cannot be used to rebuild the model. The output is always strict JSON: any number that is not finite (NaN or infinite, for example the final_objective of a fit that diverged) is written as null, so it round-trips through any standard JSON parser. The keys are:

  • skgp_version: the version of skgp that fitted the model. A model saved with pickle and loaded elsewhere keeps the version that fitted it, which may differ from the installed one.

  • training_rows: how many rows of data the model was fitted to.

  • setup: what you asked for. features (the input columns, in order), date_features (only when there are date inputs: each of them with its unit, "days since 1970-01-01"), target (the target column), kernel: its name, or for a GPJax kernel its class, with a sum or product of kernels written out, such as "RBF + Periodic", mean_function: its name, or for a GPJax mean function its class, written out as a kernel’s is, learned_noise: "constant", "heteroscedastic" or null, and noise_column (only for a model with known noise: the column that holds it).

  • learned: what fitting found out about the data.

    • kernel: every value of the fitted kernel, by its place in the kernel, as GPJax names it: lengthscale and variance for a kernel by name, and kernels[1].period for the period of the second kernel of a sum. A value with one number per input is given for each input column, by its name; a single number for all of them is one number. A length scale is how far you have to move along an input, in the column’s own units, before the curve changes appreciably: larger means a smoother curve along it. A period is in the column’s units too; the length scale of a Periodic kernel measures a fraction of the period instead, and has no units. A variance is relative to the target’s variance: 1 means the curve varies as much as the target does. A value with other units, such as the variance of a Linear kernel, is in the column’s units for the input and relative to the target’s variance for the curve.

    • mean_function: only for a mean function with values, so not for "zero". For "constant", constant: the curve far from the data, in the target’s units. For "linear", intercept: the curve where every input is 0, in the target’s units (at 1970-01-01 along a date input), and slope: for each input column, by its name, how much the curve rises per unit of the column (per day along a date input), in the target’s units. For a GPJax mean function, every value of it, by its place in it, as for kernel, in the units it sees: the columns’ and the target’s.

    • noise: only for a model that learns noise. The standard deviation of the learned noise on each observation, in the target’s units (how noisy the data looks). With known noise, it is the noise the fit learned on top of each row’s known noise. With heteroscedastic noise, it is the median noise far from the training data, where the noise process (the logarithm of the noise variance) has its learned mean: the exponential of half that mean, times the target’s standard deviation. A prediction there adds more noise than this, because it also counts how unsure the noise process is.

    • noise_kernel: only with heteroscedastic noise. Every value of the noise kernel, the "matern52" kernel of the noise process, named as in kernel. A length scale is how far you have to move along an input, in the column’s own units, before the noise changes appreciably. The variance is that of the noise process, which has no units: larger means the noise varies more.

  • fitting: how the search for the learned values ended.

    • finished_properly: only for a model fitted on all rows at once. True if the fit finished, false if it did not, in which case the learned values may be poor. It is judged from where the fit ended, not from what the optimiser reported: true means further steps would no longer change the fit by much, whether the optimiser stopped by itself or used all its iterations. A sparse model fitted on minibatches runs every one of its iterations, and skgp does not judge whether it converged.

    • iterations: how many iterations the fit took: at most the iterations given to fit for a model fitted on all rows at once, and exactly that many for a sparse model fitted on minibatches. There an iteration looks at one minibatch, and a pass through the data is as many iterations as it takes to see every row once on average.

    • final_objective: how well the fit explains the rescaled data at the end, as a number where lower is better. For an exact model it is the negative log marginal likelihood. For a sparse model fitted on all rows, or any model with heteroscedastic noise, it is the negative of a lower bound on that likelihood, so a little above what an exact fit would give. For a sparse model fitted on minibatches it is the negative of the same kind of bound, scaled to all the rows and averaged over the iterations of the last pass; each one sees only its minibatch, so it is noisy. It is on the rescaled data, so it is useful for comparing fits of the same data and settings, not as a value in your units.

    • stop_reason: only for a model fitted on all rows at once. Why the fit stopped, as a sentence in plain words, the same one the ConvergenceWarning gives. The optimiser’s own text is only in the verbose log, in the optimiser_message field of fit finished.

  • model: only for a model made by a SparseGPRegressor.

    • class: "SparseGPRegressor".

    • num_inducing: how many inducing points the fit used (never more than the number of rows).

    • minibatch_size: the minibatch size, or null for the fit on all rows at once.

    • random_state: the whole number that seeded the fit.

property gpjax_model: Any#

The underlying fitted GPJax model, for cases skgp does not cover.

See docs/advanced.md.

model: GPRegressor | SparseGPRegressor#

The model description that was fitted, with every setting it was fitted with.