# Table predict

> Fit ChakraTab on a table and predict rows, in one call.

`POST /v1/table/predict`

Fits on `train` and predicts `test` in one call, and keeps nothing afterwards. To predict repeatedly from one fit, use
[/v1/table/fits](https://yhatlabs.com/models/docs/api/fits). This learns whether a customer bought from ten rows and predicts two new ones:

```python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from yhatlabs import Client

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

yh = Client()
model = yh.fit(X_train.assign(diagnosis=y_train), target="diagnosis")
res = model.predict(X_test)
print(res.df.head(3).round(3))
print("accuracy:", round((res.predictions == y_test).mean(), 3))
model.delete()
```

Output:

```
 prediction    p_0    p_1
512           0  1.000  0.000
457           1  0.013  0.987
439           1  0.000  1.000
accuracy: 0.979
```

```bash
curl -s https://api.yhatlabs.com/v1/table/predict \
  -H "Authorization: Bearer $YHAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"target": "bought",
   "train": [{"visits": 1, "plan": "free", "bought": 0}, {"visits": 9, "plan": "pro", "bought": 1},
             {"visits": 2, "plan": "free", "bought": 0}, {"visits": 7, "plan": "pro", "bought": 1},
             {"visits": 3, "plan": "free", "bought": 0}, {"visits": 8, "plan": "pro", "bought": 1},
             {"visits": 1, "plan": "pro", "bought": 0}, {"visits": 6, "plan": "free", "bought": 1},
             {"visits": 2, "plan": "pro", "bought": 0}, {"visits": 9, "plan": "free", "bought": 1}],
   "test": [{"visits": 8, "plan": "pro"}, {"visits": 1, "plan": "free"}]}' \
  | python3 -c "import sys, json; r = json.load(sys.stdin); print(r['predictions'], r['classes'], r['probabilities'])"
```

Output:

```
[1, 0] [0, 1] [[0.000736, 0.999264], [0.999706, 0.000294]]
```

## Request

<!-- 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. |
| `test` | table | **required** | Rows to predict, with the same feature columns. |
| `quantiles` | array of number | optional | Regression: interval levels, 1 to 32. (at most 32) |
| `set_coverage` | number | optional | Classification: prediction sets with this coverage. |
| `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. |

**URL parameters:** `model`, `async` and `store` can also go on the URL.

| Parameter | Example | Same as |
|---|---|---|
| `model` | `?model=chakra-tab-v2` | `model` in the body. Naming one model in the URL and another in the body answers `400`. |
| `async` | `?async=true` | `"async": true`: run the call as a [job](https://yhatlabs.com/models/docs/api/jobs). |
| `store` | `?store=false` | `"store": false`: keep the call out of your Logs. The `X-Store: false` header does the same. |

A **table** is a list of row objects, `{"format": "parquet_b64", "bytes": "<base64 parquet>"}`, or an uploaded table
`{"format": "table", "id": "tbl_...", "rows": [...], "drop": [...]}` ([/v1/uploads](https://yhatlabs.com/models/docs/api/uploads)). Up to 200,000 rows
in a call, and 64 MB per request body.

**Presets:** start on `fast`, which takes seconds on a few thousand rows; `medium` is more accurate, about 3x the credits and
2-3x as long; `pro` is the most accurate, about 8x the credits and 4-8x as long as `fast` (`full` is still accepted, as `pro`).

**Size:** a direct call takes up to 100,000 training rows x 100 columns on `fast`, and 25,000 x 100 on `medium` and `pro`.
Past that it answers `413` with error type `async_required`: send it with `"async": true` to run it as a
[job](https://yhatlabs.com/models/docs/api/jobs). A direct call that runs longer than about 20 minutes comes back as a job object
(`"object": "job"`) to poll.

**Logs:** `"store": false` and the call isn't shown in your Logs ([Data handling](https://yhatlabs.com/models/docs/resources/data-handling)).

**Large requests:** a request body over 4 MB is stored as it arrives and runs as a job, so its size caps are a job's
(50,000,000 training cells); the reply still comes back directly, or as a job after about 20 minutes. Its fields and uploaded
tables work as in any request. For large tables, [upload](https://yhatlabs.com/models/docs/api/uploads) them once and refer to them: the Python
library does this for any table over 8 MB.

## Response

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

| Field | Type | Description |
|---|---|---|
| `predictions` | array of number or string or boolean | One per test row: a number, or a class label in its own type. |
| `probabilities` | array of array of number | Classification: one row per test row, one column per class. |
| `classes` | array of number or string or boolean | Classification: the class of each probabilities column. |
| `quantile_levels` | array of number | Regression with quantiles: the levels. |
| `quantiles` | array of array of number | Regression with quantiles: one row per test row. |
| `interval_method` | `distribution`, `conformal` | distribution: each row's own predictive distribution; conformal: the same width for every row (fallback). |
| `mean` | array of number | Of each row's predictive distribution. |
| `median` | array of number |  |
| `prediction_sets` | array of array of number or string or boolean | Classification with set_coverage: the classes per row that hold the true one that often. |
| `set_coverage` | number | The coverage the sets were built for. |
| `restored` | boolean | Fits only: the fit was rebuilt from its training table before predicting (it had to be loaded again). |
| `fit` | object | One-shot predict only: rows, features, seconds, problem type, metric, preset, seed, version. |
| `usage` | object | Predicted rows, and the credits the call cost (up to 3 decimals, e.g. 0.02). |

Class labels come back in their own type: a class trained as `1` is `1`, and `true` is `true`. The call costs the credits of a fit
plus a prediction ([Limits](https://yhatlabs.com/models/docs/resources/limits)); `usage.credits` says how many.

## Compare with standard models

`POST /v1/table/compare`

Trains standard models on the same `train` and predicts the same `test`, so ChakraTab can be scored against them on equal
terms. It takes `train`, `target` and `test` as above (a `test` that still holds the target column is fine; it is ignored),
`problem_type`, `store`, and `models`: a list of `xgboost`, `random_forest`, `linear` (the default three) and `lightgbm`.

The reply has each model's `predictions` (and `probabilities` for a classification), its training time `fit_s`, and a naive
`baseline` (the most common class, or the mean). Scores are left to you, so every model is scored the same way; in Python,
`res.comparison.score(actual, ChakraTab=res)` does it ([Evaluating](https://yhatlabs.com/models/docs/tabular/evaluating#compare-with-standard-models)).
It is a direct call, up to 100,000 training rows x 100 columns, and costs training rows x feature columns x 0.5 plus the
predicted cells, in tokens ([Limits](https://yhatlabs.com/models/docs/resources/limits#pricing)).
