# opencr.ml.surrogate

*module*

A baseline surrogate of the CR solver with scikit-learn.

`Surrogate` learns ``ln(outputs)`` from ``ln(inputs)`` of a dataset
(`generate_dataset`), by default with a Gaussian
process (a neural network is the optional next step). The solver stays the reference: `Surrogate.evaluate`
reports the held-out error against it and the speed-up, and
`Surrogate.predict` flags inputs outside the training ranges (out of
domain), where the surrogate is not to be trusted. scikit-learn is imported
only here (the ``ml`` extra).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L1-L1)

## opencr.ml.surrogate.Prediction

*class* · *dataclass*

```python
class Prediction
```

Surrogate outputs and whether each input is inside the training domain.

**Attributes**

- `values` (`np.ndarray`) — The predicted outputs (sample, output).
- `in_domain` (`np.ndarray`) — True where every input is inside the training ranges.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L43-L53)

### opencr.ml.surrogate.Prediction.in_domain

*attribute* · *instance attribute*

```python
in_domain: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L53-L53)

### opencr.ml.surrogate.Prediction.values

*attribute* · *instance attribute*

```python
values: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L52-L52)

## opencr.ml.surrogate.Surrogate

*class*

```python
class Surrogate
```

A regression of the dataset's outputs on its inputs, in log space.

**Parameters**

- `estimator` (`Any`) (default: `None`) — A scikit-learn regressor with multi-output ``fit`` and ``predict``; by default a Gaussian process with an anisotropic RBF kernel on standard-scaled inputs (smooth, few samples).
- `seed` (`int`) (default: `0`) — The seed of the default estimator.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L56-L141)

### opencr.ml.surrogate.Surrogate.estimator

*attribute* · *instance attribute*

```python
estimator = estimator
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L68-L68)

### opencr.ml.surrogate.Surrogate.high

*attribute* · *instance attribute*

```python
high = np.empty(0)
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L73-L73)

### opencr.ml.surrogate.Surrogate.low

*attribute* · *instance attribute*

```python
low = np.empty(0)
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L72-L72)

### opencr.ml.surrogate.Surrogate.outputs

*attribute* · *instance attribute*

```python
outputs: tuple[str, ...] = ()
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L71-L71)

### opencr.ml.surrogate.Surrogate.parameters

*attribute* · *instance attribute*

```python
parameters: tuple[str, ...] = ()
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L70-L70)

### opencr.ml.surrogate.Surrogate.provenance

*attribute* · *instance attribute*

```python
provenance: dict[str, Any] = {}
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L74-L74)

### opencr.ml.surrogate.Surrogate.seed

*attribute* · *instance attribute*

```python
seed = seed
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L69-L69)

### opencr.ml.surrogate.Surrogate.evaluate

*method*

```python
def evaluate(dataset: xr.Dataset, split: int = 2, solve_time: float | None = None) -> xr.Dataset
```

Return the held-out relative errors and the speed-up on one split.

**Parameters**

- `dataset` (`xr.Dataset`) — The dataset the surrogate was trained on (or a new one from the same model).
- `split` (`int`) (default: `2`) — 1 for validation, 2 for test.
- `solve_time` (`float | None`) (default: `None`) — The solver's time per sample in s, for the speed-up; by default the dataset's ``solve_time``.

**Returns**

- (`xr.Dataset`) — Per output: the median, 95th percentile and maximum of
- (`xr.Dataset`) — ``|predicted / solved - 1|``, and in the attributes the surrogate's
- (`xr.Dataset`) — time per sample and the speed-up.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L105-L141)

### opencr.ml.surrogate.Surrogate.fit

*method*

```python
def fit(dataset: xr.Dataset) -> Surrogate
```

Train on the dataset's training split and return ``self``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L76-L89)

### opencr.ml.surrogate.Surrogate.forward

*method*

```python
def forward(theta: np.ndarray) -> np.ndarray
```

One sample's outputs, as a forward model for `opencr.inference`.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L101-L103)

### opencr.ml.surrogate.Surrogate.predict

*method*

```python
def predict(inputs: np.ndarray) -> Prediction
```

Return the outputs at ``inputs`` (sample, parameter) with the domain flag.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L91-L99)
