# Fitting models

by: Chris Cave & Jaan Kasak

(Feyn version 2.0 or newer)

Each `Model`

is a mathematical function that can be optimised to a **loss** function in order to fit to a dataset. Fitting happens on the client side, and no communication occurs with the `QLattice`

in this step.

Here's an example of how to sample and fit models.

```
import feyn
from feyn.datasets import make_classification
train, test = make_classification()
ql = feyn.connect_qlattice()
models = ql.sample_models(train.columns, 'y', 'classification', max_complexity=10)
models = feyn.fit_models(
models=models,
data=train,
loss_function='binary_cross_entropy',
criterion='bic',
threads=4
)
```

## models

This is the list of `Model`

s you want to fit the data to. This will return the same list but sorted by ascending loss.

## data

This is the data that the `Model`

s will be fitted to. This needs to be a `pandas.DataFrame`

object where a sample is represented by a single row and the variable names are the names of the columns.

## loss_function

This is the **loss** function that the `Model`

s are optimised for. If no **criterion** is given, the returned list of `Model`

s is sorted by this **loss** function in ascending order. It takes any **loss** function in the `feyn.losses`

module. Popular choices of **loss** functions are:

**binary_cross_entropy****squared_error**

## criterion

The **criterion** takes into account the complexity of the `Model`

when comparing losses. Higher complexity is penalized. Choices of **criterion** are:

**bic**(Bayesion Information Criterion)**aic**(Akaike Information Criterion)

**bic** penalises complex models more than **aic**.

If a **criterion** is given, the returned list of `Model`

s are sorted by that instead of the loss.

## threads

Determines how many concurrent threads to use during fitting. For best effect, set the **threads** parameter to match the number of threads available on your computer.