# Fits

> Fit a table once, then predict from the stored fit and compute importance.

| Method and path | What it does |
|---|---|
| `POST /v1/table/fits` | creates a fit |
| `POST /v1/table/fits/{id}/predict` | predicts rows from it |
| `POST /v1/table/fits/{id}/importance` | column importance |
| `GET /v1/table/fits`, `GET /v1/table/fits/{id}` | lists the project's fits, reads one |
| `PATCH /v1/table/fits/{id}` | renames a fit, or keeps it longer |
| `DELETE /v1/table/fits/{id}` | deletes a fit |

A fit is stored on our side until it expires or you delete it, so each later call sends only the rows to score. This creates a
fit, predicts two rows from it and deletes it:

```bash
# fit once
FIT=$(curl -s https://api.yhatlabs.com/v1/table/fits \
  -H "Authorization: Bearer $YHAT_API_KEY" -H "Content-Type: application/json" \
  -d '{"target": "y", "problem_type": "regression", "ttl_hours": 1,
   "train": [{"x": 1, "y": 2.1}, {"x": 2, "y": 3.9}, {"x": 3, "y": 6.2}, {"x": 4, "y": 8.1},
             {"x": 5, "y": 9.8}, {"x": 6, "y": 12.2}, {"x": 7, "y": 13.9}, {"x": 8, "y": 16.1},
             {"x": 9, "y": 18.0}, {"x": 10, "y": 19.9}]}' \
  | python3 -c "import sys, json; print(json.load(sys.stdin)['id'])")
echo "fit: ${FIT:0:8}..."

# predict from it, as often as you like
curl -s https://api.yhatlabs.com/v1/table/fits/$FIT/predict \
  -H "Authorization: Bearer $YHAT_API_KEY" -H "Content-Type: application/json" \
  -d '{"test": [{"x": 11}, {"x": 12}]}' \
  | python3 -c "import sys, json; r = json.load(sys.stdin); print(r['predictions'], r['usage'])"

# delete it when done
curl -s -X DELETE https://api.yhatlabs.com/v1/table/fits/$FIT -H "Authorization: Bearer $YHAT_API_KEY" \
  | python3 -c "import sys, json; print(json.load(sys.stdin)['deleted'])"
```

Output:

```
fit: fit_4c47...
[21.531238555908203, 23.052967071533203] {'rows': 2, 'credits': 0.02}
True
```

## Create a fit

`POST /v1/table/fits`

It takes:

<!-- generated by scripts/build_api_tables.py from the OpenAPI spec; do not edit -->

| Field | Type | Required / default | Description |
|---|---|---|---|
| `train` | table | **required** | Rows including the target column. |
| `target` | string | **required** | The column to predict. |
| `name` | string | optional | A name to find it by. (max length 120) |
| `async` | boolean | optional | Queue the fit and answer 202 with a job; GET /v1/jobs/&#123;id&#125; gives the fit when done. |
| `ttl_days` | number | `30` | Days the fit is kept after its last use; each prediction starts the count again. (max 365) |
| `model` | string | `"chakra-tab"` | The model, as GET /v1/models lists it. |
| `preset` | `fast`, `medium`, `pro`, `full` | `"fast"` | fast: the quickest; medium: more accurate, about 3x the credits and 2-3x the time; pro: the most accurate, about 8x the credits and 4-8x the time (full is the same, its earlier name). |
| `problem_type` | `binary`, `multiclass`, `regression` | optional | Inferred from the target when left out. |
| `eval_metric` | string | optional | What validation optimises: log_loss (default), roc_auc, f1, ...; rmse (default), mae, r2. |
| `time_limit` | number | optional | Seconds for the whole call. |
| `seed` | integer | `0` | Random seed; the same seed gives the same fit. |
| `store` | boolean | `true` | false keeps this call out of your Logs (we still keep a copy to look into errors; never used for training). |
| `groups` | string | optional | A train column: validation folds hold out whole groups; not used as a feature. |
| `time_col` | string | optional | A train column: validation folds are contiguous periods. |

It returns:

<!-- generated by scripts/build_api_tables.py from the OpenAPI spec; do not edit -->

| Field | Type | Description |
|---|---|---|
| `id` | string | fit_... |
| `object` | `"fit"` |  |
| `model` | string |  |
| `target` | string |  |
| `created_at` | integer | Unix seconds. |
| `expires_at` | integer | Unix seconds; the fit is deleted then. |
| `fit` | object | Rows, features, seconds, problem type, metric, preset, seed, version, groups, time_col. |
| `classes` | array of number or string or boolean | Classification: the classes. |
| `intervals` | boolean | Regression: whether quantiles= is available. |
| `prediction_sets` | boolean | Classification: whether set_coverage= is available. |
| `usage` | object |  |

## Predict from a fit

`POST /v1/table/fits/{id}/predict`

It takes:

<!-- generated by scripts/build_api_tables.py from the OpenAPI spec; do not edit -->

| Field | Type | Required / default | Description |
|---|---|---|---|
| `test` | table | **required** | Rows to predict, with the fit's feature columns. |
| `quantiles` | array of number | optional | Regression: interval levels. (at most 32) |
| `set_coverage` | number | optional | Classification: prediction sets with this coverage. |

and returns the prediction fields of
[/v1/table/predict](https://yhatlabs.com/models/docs/api/tabular-predict#response), plus `fit_id` and `restored`. `restored: true` means the fit
was rebuilt from its stored training table during this call (same seed, so the same model): slower, same result.

A prediction is charged on the fit's training table (0.5 x its cells) plus the predicted cells, so scoring a few rows from a
large fit still costs a share of it. For a regression, `quantiles` (ranges) add a fixed cost, and about 10-20 seconds the first
time they are asked of a fit. `predictions` is the point forecast to use; with `quantiles`, `median` and `mean` describe each
row's predicted distribution.

## Importance

`POST /v1/table/fits/{id}/importance`

It takes, all fields optional:

<!-- generated by scripts/build_api_tables.py from the OpenAPI spec; do not edit -->

| Field | Type | Required / default | Description |
|---|---|---|---|
| `data` | table | optional | Labeled rows the fit has not seen (target included); the most honest numbers. |
| `subsample_size` | integer | `1000` | Rows per shuffle. (max 20000) |
| `num_shuffle_sets` | integer | `3` | Repeats per column. (max 20) |
| `time_limit` | number | optional | Seconds. |

It returns:

<!-- generated by scripts/build_api_tables.py from the OpenAPI spec; do not edit -->

| Field | Type | Description |
|---|---|---|
| `fit_id` | string |  |
| `metric` | string |  |
| `source` | `data`, `validation`, `train` | Which rows were shuffled; train is in-sample, so optimistic. |
| `features` | array of object |  |
| `usage` | object |  |

`features` runs from most to least important.

## List, get and delete

| | |
|---|---|
| `GET /v1/table/fits` | the project's live fits: `{"object": "list", "data": [...]}` |
| `GET /v1/table/fits/{id}` | one fit |
| `PATCH /v1/table/fits/{id}` | `{"name": ..., "ttl_days": ...}`: renames it, or changes how long it is kept after its last use |
| `DELETE /v1/table/fits/{id}` | deletes the fit and its stored training table: `{"id": ..., "deleted": true}` |

A fit belongs to its project: every key of the project, and the playground, can predict from it; it shows on the project's
Saved models page. Only whoever made it, or an admin of the project, may rename or delete it. A fit lists:

| Field | |
|---|---|
| `target`, `classes` | the column it predicts, and its classes for a classification |
| `columns` | the inputs it needs, each `{"name", "type"}` with type `number` or `category`; extra columns are left out when you predict, a missing one fails the call |
| `made_by` | `{"user_id", "key_id", "playground"}`: the member who made it (null for a production key) |
| `ttl_days`, `last_used_at`, `expires_at` | kept `ttl_days` (30 by default, 1 to 365) after its last use |
| `saved`, `saved_bytes` | whether the trained model itself is stored (it reloads in seconds) |

An expired or deleted fit returns `404` with type `fit_not_found`. Every call states its cost in `usage.tokens` and
`usage.credits` ([Limits](https://yhatlabs.com/models/docs/resources/limits#pricing)). A fit past its preset's direct size (`fast` 100,000 training
rows x 100 columns, `medium` and `pro` 25,000 x 100) answers `async_required`: send it with `"async": true` to run it as an
[async job](https://yhatlabs.com/models/docs/api/jobs).
