Metadata-Version: 2.3
Name: skgp
Version: 0.0.2
Summary: Easy fitting of Gaussian process models.
Author: Thomas Pinder
Author-email: Thomas Pinder <tompinder@live.co.uk>
Requires-Dist: equinox>=0.13.8
Requires-Dist: gpjax>=1.1.0
Requires-Dist: jax>=0.10.2
Requires-Dist: loguru>=0.8.0
Requires-Dist: narwhals>=2.28.0
Requires-Dist: numpy>=2.4.6
Requires-Dist: optax>=0.2.9
Requires-Dist: paramax>=0.0.5
Requires-Dist: polars>=2.1.0
Requires-Dist: pydantic>=2.14.3
Requires-Dist: scipy>=1.17.1
Requires-Python: >=3.12
Description-Content-Type: text/markdown

# skgp

Fit Gaussian process models from dataframes, without learning JAX.

A Gaussian process is a flexible way to learn a smooth curve from data and to say how uncertain you are about it. The libraries that fit them well ask you to build several pieces by hand. skgp does that setup for you: you describe the model with your column names, call `.fit()` and `.predict()`, and get answers back as dataframes. It is built for QuantClimate's own work first, and for anyone else who finds it useful.

## Status

The code in this repository does what this README says, and the examples below run as part of the test suite. skgp is in early development on the 0.x line, so the API can change between minor versions (0.1 to 0.2, for example).

Install it from PyPI:

```bash
pip install skgp
```

## Quickstart

This example makes up a small table of temperatures, fits a model to it, and predicts for two new places.

```python
import numpy as np
import polars as pl
from skgp import GPRegressor

# Made-up data: temperature falls with latitude and altitude, plus some noise.
rng = np.random.default_rng(0)
latitude = rng.uniform(35, 60, 40)
altitude = rng.uniform(0, 2000, 40)
temperature = 30 - 0.4 * (latitude - 35) - 0.006 * altitude + rng.normal(0, 0.5, 40)
train = pl.DataFrame(
    {"latitude": latitude, "altitude": altitude, "temperature": temperature}
)
new = pl.DataFrame({"latitude": [45.0, 52.0], "altitude": [100.0, 800.0]})

model = GPRegressor(features=["latitude", "altitude"], target="temperature")
fitted = model.fit(train)

prediction = fitted.predict(new)
print(
    prediction.to_frame()
)  # your rows, plus the predicted mean and standard deviation
print(prediction.interval(0.95))  # lower and upper bounds for each row
```

`prediction.to_frame()` gives back every column you passed, followed by `mean` and `std`, and `prediction.to_frame(interval=0.95)` adds `lower` and `upper` too. `prediction.interval()` uses a 95% level if you give none. Pass pandas dataframes instead of Polars ones and you get pandas dataframes back.

If your rows already have a column called `mean`, `std`, `lower` or `upper`, pass `suffix=` to `predict`, for example `fitted.predict(new, suffix="_predicted")`. Every column skgp adds then gets that ending (`mean_predicted` and so on).

skgp has no save function of its own. To keep a fitted model, save it with Python's `pickle`, as you would a scikit-learn model; [docs/saving.md](docs/saving.md) shows how, and what to watch for.

To see what skgp checks and chooses, add `verbose=True` to `check`, `fit` or `predict`:

```python
fitted = model.fit(train, verbose=True)
```

## Large datasets

`GPRegressor` fits an exact model when its noise is constant, the default. The work it does grows with the cube of the number of rows and the memory it needs grows with the square, so it becomes impractical somewhere past ten thousand rows. For a table that large, use `SparseGPRegressor`. It takes the same `features`, `target`, `kernel`, `noise` and `schema`, and you call the same `check`, `fit` and `predict`. The difference is how it learns: it summarises your data with a smaller set of points (called inducing points) and fits that summary. That is an approximation, so it trades some accuracy for speed and memory. skgp never chooses it for you: a `GPRegressor` never summarises your data, and a `SparseGPRegressor` always does. Noise is always Gaussian.

This example makes up 1,000 places instead of 40, and fits them in minibatches of 250 rows:

```python
from skgp import SparseGPRegressor

# The same made-up temperatures as above, for 1,000 places.
big_latitude = rng.uniform(35, 60, 1000)
big_altitude = rng.uniform(0, 2000, 1000)
big_temperature = (
    30 - 0.4 * (big_latitude - 35) - 0.006 * big_altitude + rng.normal(0, 0.5, 1000)
)
big_train = pl.DataFrame(
    {"latitude": big_latitude, "altitude": big_altitude, "temperature": big_temperature}
)

sparse = SparseGPRegressor(
    features=["latitude", "altitude"], target="temperature", minibatch_size=250
)
sparse_fitted = sparse.fit(big_train)
print(sparse_fitted.predict(new).to_frame())  # the same kind of result as before
```

`fit` returns the same `FittedGPRegressor` as before, so `.predict()` and `.to_json()` work in the same way. Besides the settings it shares with `GPRegressor`, a `SparseGPRegressor` has three:

- `minibatch_size`: how the fit goes through your data. Leave it out (the default) and each step looks at all the rows at once, which gives the most accurate sparse fit and no randomness from batching. Give a whole number above zero and each step looks at a random batch of that many rows, which suits data too large to process at once. Zero or a negative number is refused when you define the model.
- `num_inducing`: how many points summarise the data, a whole number above zero. Leave it out and skgp uses 10% of the rows, but never fewer than 100 or more than 1,000. If that number, or yours, is more than the number of rows, every row is used. The points start at the centres of a k-means clustering of your rescaled inputs and are moved during the fit.
- `random_state`: a whole number from 0 to 9,223,372,036,854,775,807, 0 unless you change it, that seeds everything random in the fit (where the points start and the order of the minibatches). The same data and settings always give the same model; change it to see how much a fit depends on it.

How the fit ends depends on `minibatch_size`. Without one, it stops the way an exact fit does, and a fit that stops early gives the same `skgp.ConvergenceWarning`. With one, the fit runs exactly `iterations` iterations, each on one minibatch: 2,000 unless you pass another number, for example `sparse.fit(big_train, iterations=5_000)`. skgp does not judge whether such a fit converged: it issues no `skgp.ConvergenceWarning`, and `to_json()` gives the iterations it ran and its final objective, with no `finished_properly`.

`fitted.to_json()` of a sparse model also has a `model` section: the class that made it, the number of inducing points used, the minibatch size (null when there is none) and the `random_state`. With `verbose=True`, `fit` also logs whether the fit is on all rows or on minibatches, how many inducing points and whether you chose that number, how they start (before it searches for them), the `random_state`, and, for a fit on all rows, why fitting stopped. A fit on minibatches also shows GPJax's progress bar.

A `GPRegressor` that is given a lot of rows warns you. Its `fit` issues a `skgp.LargeDataWarning` (a `UserWarning`) when the data has more rows than `skgp.config.size_warning_rows`, which is 10,000 to start. The fit then carries on and gives the same model it always would. Assign to the setting to change the limit, for example `skgp.config.size_warning_rows = 50_000`. It controls only the warning, never which model is fitted, and `SparseGPRegressor` never issues it. The reasons for this design are in [ADR 0004](docs/adr/0004-sparse-models-are-a-separate-class.md).

## Classification

`GPClassifier` predicts which of two classes each row belongs to, and how likely each class is. It takes the same `features`, `kernel` and `schema` as `GPRegressor`, and you call the same `check`, `fit` and `predict`. Its `target` may hold any two different values: numbers, text, or true and false.

This example makes up whether it rained at 40 places, more often further north, and predicts the chance of rain at two new places:

```python
from skgp import GPClassifier

# Made-up data: rain is more likely further north.
rain_latitude = rng.uniform(35, 60, 40)
rained = rng.uniform(size=40) < 1 / (1 + np.exp(-(rain_latitude - 47.5) / 3))
weather = pl.DataFrame(
    {"latitude": rain_latitude, "weather": np.where(rained, "rain", "dry")}
)

classifier = GPClassifier(features="latitude", target="weather")
classifier_fitted = classifier.fit(weather)
rain_prediction = classifier_fitted.predict(pl.DataFrame({"latitude": [40.0, 55.0]}))
print(
    rain_prediction.to_frame()
)  # your rows, each class's probability, the likelier class
```

`rain_prediction.to_frame()` gives your rows, then `probability_dry` and `probability_rain`, then `most_likely_class`. `rain_prediction.probabilities()` gives the same probabilities as a numpy array with one column per class, in the order of `rain_prediction.classes`, and `rain_prediction.most_likely()` gives the likelier class of each row. skgp codes your two values as 0 and 1 in sorted order (the smaller number, false, or the text that comes first alphabetically is 0), and with `verbose=True`, `fit` logs which is which.

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. A row far from the training data gets probabilities nearer 0.5. The model is fitted by variational inference with an inducing point at every training row, so, like an exact regression model, it gets slow as rows grow (about 20 seconds at 500 rows and two and a half minutes at 1,000), and `fit` issues a `skgp.LargeDataWarning` above `skgp.config.size_warning_rows`. The fit stops once it has finished, after at most `iterations` iterations: 2,000 unless you pass another number. `classifier_fitted.to_json()` lists the two classes and what the fit learned: the kernel's values, such as a length scale along each input and a variance, and no noise. The reasons for this design are in [ADR 0006](docs/adr/0006-the-classifier-uses-variational-inference-at-every-row.md).

For a large table, use `SparseGPClassifier`. It is to `GPClassifier` what `SparseGPRegressor` is to `GPRegressor`: it takes the same settings plus `minibatch_size`, `num_inducing` and `random_state`, which work as described under [Large datasets](#large-datasets), and its `fit` returns the same kind of fitted model and prediction. Without a `minibatch_size` it fits on all rows at once with the same optimiser as `GPClassifier`, stopping once it has finished. With one, it runs exactly `iterations` iterations, each on one minibatch, as a `SparseGPRegressor` does. Its `to_json()` has the same `model` section as a sparse regressor's, and it never issues a `LargeDataWarning`. skgp never switches to it for you.

## Your own kernel

`kernel` takes a name or a GPJax kernel. The names are `"matern52"` (the default) for a smooth curve, `"matern32"` for a rougher one and `"rbf"` for a very smooth one, each with one length scale per input. For any other shape of curve, such as one that repeats, pass a kernel from GPJax's `gpjax.kernels`, alone or added and multiplied together. This example fits three years of weekly sales with a slow trend plus a season that repeats every year:

```python
import gpjax as gpx

# Made-up data: weekly sales that rise slowly and repeat every 52 weeks.
week = np.arange(156.0)
season = 80 * np.sin(2 * np.pi * week / 52)
sales = 1000 + 0.5 * week + season + rng.normal(0, 10, 156)
weekly = pl.DataFrame({"week": week, "sales": sales})

# A slow trend plus a season, written in weeks, the units of the week column.
kernel = gpx.kernels.RBF(lengthscale=100.0) + gpx.kernels.Periodic(period=50.0)
seasonal = GPRegressor(features="week", target="sales", kernel=kernel)
seasonal_fitted = seasonal.fit(weekly)
print(seasonal_fitted)  # the learned period, kernels[1].period, is close to 52 weeks
```

Every kernel sees your inputs in the units of your columns, so the length scale and the period above are in weeks, both as you give them and as the fit reports them. The values you give are where the fit starts. The fit learns all of them, except any you hold with `paramax.non_trainable`, as in GPJax. A kernel is taken as you give it: `gpx.kernels.RBF()` has one length scale for all its inputs, and `gpx.kernels.RBF(lengthscale=jnp.array([1.0, 500.0]))` one for each of two. A kernel's `active_dims` are positions in `features`, counting from 0. A named kernel's length scales start at each input's standard deviation.

A regressor's kernel variance is relative to the target's variance: 1 means the curve varies as much as the target does. Printing a fitted model and `to_json()` name each learned value 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.

## Noise that varies

By default a regressor learns one noise level for every row. With `noise="heteroscedastic"`, on a `GPRegressor` or a `SparseGPRegressor`, it learns noise that varies with the inputs, for a measurement that gets noisier in some places than in others:

```python
# Made-up data: a measurement that gets noisier as x grows.
x = np.sort(rng.uniform(0, 10, 150))
y = 3 * np.sin(x) + (0.1 + 0.3 * x) * rng.normal(size=150)
noisy = pl.DataFrame({"x": x, "y": y})

varying = GPRegressor(features="x", target="y", noise="heteroscedastic")
varying_fitted = varying.fit(noisy)

ends = pl.DataFrame({"x": [1.0, 9.0]})
observation = varying_fitted.predict(ends).std()
curve = varying_fitted.predict(ends, include_noise=False).std()
print(
    (observation**2 - curve**2) ** 0.5
)  # the noise at x = 1, then the larger noise at x = 9
```

A prediction's spread includes the noise the model expects at each row, unless you ask for the curve alone with `include_noise=False`; the difference of the two variances is the noise variance at that row. The logarithm of the noise variance is a second Gaussian process, the noise process, with a `"matern52"` kernel of its own, the noise kernel, learned together with the curve. Printing a fitted model and `to_json()` give `noise`, the median noise far from the training data, and `noise_kernel`, the values of the noise kernel, such as its length scale along each input in your units.

GPJax has no exact fit of this model. A `GPRegressor` fits it by variational inference with an inducing point at every training row for the curve and for the noise process, as a `GPClassifier` is fitted, so it gets slow as rows grow and warns above `skgp.config.size_warning_rows`. A `SparseGPRegressor` gives each process `num_inducing` inducing points; on minibatches it fits with Adam alone, as GPJax takes no natural-gradient step on this model. The reasons for this design are in [ADR 0007](docs/adr/0007-heteroscedastic-noise-is-fitted-by-variational-inference.md).

## How it behaves

- **Dataframes in, dataframes out.** You never import JAX. Polars is the main dataframe, and any common dataframe, pandas included, works through [narwhals](https://narwhals-dev.github.io/narwhals/). Results come back in the type you passed in.
- **Nothing changes in place.** `GPRegressor` describes a model: the input columns (`features`), the `target` column, and optionally a `kernel`, how the `noise` varies and a `schema`. `.fit()` returns a separate `FittedGPRegressor` (a `FittedGPClassifier` for a `GPClassifier`), and only that has `.predict()`. `model.replace(kernel="rbf")` returns a new model with that change. You can give the inputs and the target by position, `GPRegressor(["latitude", "altitude"], "temperature")`, and a single input by its name alone.
- **Mistakes in settings are caught when you define the model.** An unknown kernel name, a kernel whose `active_dims` point past the last feature, a misspelt setting, a column named twice, the target among the features or an empty column name raises a `ValueError` straight away, with one plain line per problem that names the setting and what you gave.
- **Your data is checked first.** Missing or non-numeric columns, missing values (empty or NaN), infinite values, fewer than 5 training rows, and prediction columns that do not match each stop with a clear error that names the column or the number of rows. A classifier's target may hold values of any type, but exactly two different ones. A date or time column is refused with a note on how to turn it into numbers, for example the number of days since the first date. You can run the checks ahead of a long fit with `model.check(df)`, which returns a `CheckedData` that `fit` and `predict` accept without checking again. It is valid for any model of the same task with the same `features`, `target` and `schema`, whatever its kernel or class, so you can check once and then fit several kernels, or both an exact and a sparse model. Data checked by a regressor is refused by a classifier, and the other way round, because they check the target differently. Rows to predict can be checked ahead with `fitted.check(rows)`. They do not need a target column, and a target column among them is ignored (handy when you predict on a held-out set that includes the true values). Any other column the model does not use is ignored too, and with `verbose=True` a log record names it.
- **You can add your own checks.** Pass `schema=` an object with a `.validate(frame)` method, such as a pandera schema. It runs first, on your dataframe as you passed it, and skgp's own checks always run after it.
- **skgp handles the details.** Inputs and target are rescaled before fitting and predictions are converted back to your units. The kernel sees your inputs in their own units, so its length scales are in them. The kernel defaults to `"matern52"`; the other names are `"matern32"` and `"rbf"`, and you can pass a GPJax kernel instead (see [Your own kernel](#your-own-kernel)).
- **A fit that does not finish warns you.** If fitting does not finish properly, `fit` still returns a model but issues a `skgp.ConvergenceWarning` (a `UserWarning`) that says in plain words why it stopped, and the model may be less reliable. skgp judges this from where the fit ended, not from what the optimiser reported: a fit counts as finished when further steps would no longer change it by much, even if the optimiser gave up or ran out of iterations. Filter or escalate the warning with the `warnings` module. Only a fit on all rows at once is judged: a fit on minibatches runs the `iterations` you give it, and skgp does not say whether it converged.
- **You set how long a fit may run.** Every `fit` takes `iterations`, 2,000 unless you give another whole number above zero. For a `GPRegressor`, a `SparseGPRegressor` without a `minibatch_size`, or a `GPClassifier`, it is the most iterations the optimiser may take, and it stops sooner once the fit has finished. For a fit on minibatches, it is exactly the number of iterations run. When a fit on all rows used them all without finishing, the `ConvergenceWarning` suggests a larger `iterations`.
- **Predictions are for a new observation unless you ask for the curve.** `fitted.predict()` returns a `Prediction`. Its `.mean()` and `.std()` are numpy arrays in your original units, and by default the spread includes measurement noise. `fitted.predict(new, include_noise=False)` gives the underlying curve without the noise: the same mean, with a smaller spread. Printing a prediction says which it is and shows its table, and a notebook shows that table as a table. `.to_frame()` and `.interval()` give the results as dataframes of your type. `.covariance()` gives the n-by-n covariance between the rows as a numpy array, which tells you how the predictions move together; it costs memory that grows with the square of the number of rows, so ask for it only when you need it.
- **Repeated predictions are quick.** The first `predict` on a fitted model prepares the training data once and keeps the result, so later predictions reuse it. For a `GPRegressor` that kept result takes memory that grows with the square of the training rows (about 72 MB at 3,000 rows and 800 MB at 10,000). `predict` works through your rows 1,000 at a time; change that with `batch_size=`.
- **You can see inside the black box.** With `verbose=True`, `check`, `fit` and `predict` log each check and each choice skgp makes, as structured records through [loguru](https://loguru.readthedocs.io/). A long fit logs its iteration and objective every few seconds, and its last record gives how long the fit took and what it learned. Without `verbose`, skgp logs nothing. To send the records to Python's own `logging`, see [docs/advanced.md](docs/advanced.md#sending-the-log-records-to-logging). Printing a fitted model shows a short summary: the class that made it, its columns and kernel, the number of rows, whether the fit finished properly, and what it learned: the kernel's values, with length scales in your columns' units, and the noise in your target's. `fitted.to_json()` gives the same as JSON, with the skgp version, for inspection. You cannot rebuild a model from it.
- **Saving a model is up to you.** skgp has no save function of its own. Save a fitted model with Python's `pickle`, as you would a scikit-learn model. Only load files you trust, because loading a pickle file can run code. Load them with the same versions of skgp, GPJax and JAX that saved them, and treat the file as you treat your training data, which it holds. See [docs/saving.md](docs/saving.md).
- **GPJax stays out of the way.** skgp is built on [GPJax](https://github.com/JaxGaussianProcesses/GPJax), which does the fitting, and the underlying GPJax model is reachable from a fitted model for anyone who goes looking.

For advanced use, see [docs/advanced.md](docs/advanced.md).

## Not supported yet

Models for data on graphs, extreme-value models, fully Bayesian fitting, and random draws from predictions.

Classification is offered through `GPClassifier` and `SparseGPClassifier` (see [Classification](#classification)), with these limits:

- Two classes only. More than two are tracked in [issue 37](https://github.com/QuantClimate/skgp/issues/37).

Sparse approximations for very large datasets are offered through `SparseGPRegressor` (see [Large datasets](#large-datasets)), with these limits:

- No automatic choice. skgp does not pick between an exact and a sparse model for you, or between the two ways of fitting a sparse one ([ADR 0004](docs/adr/0004-sparse-models-are-a-separate-class.md)).
- No non-Gaussian noise in regression. A `SparseGPRegressor` takes Gaussian noise only.
- No chunked fits. Fitting exact models on pieces of the data and combining their predictions is not offered.

## Design notes

The project's vocabulary is in [GLOSSARY.md](GLOSSARY.md). Decisions that are hard to undo are recorded in [docs/adr](docs/adr).
