> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tabsutra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Recommendation

> For each customer at a date, the items they are most likely to pick in the window that follows.

A recommendation asks which rows of one table a row of another table will link to. Which products will a customer buy in the next 30 days? Which drivers will race at a circuit? The answer is a ranked list for each source, with the best first.

## The links

The links are the past events that tie a source to an item. There are two ways to give them.

* **An event table of the database.** Give `link_table`, the name of a table with a time column and a foreign key to each of the two tables. An `orders` table with `customer_id` and `product_id` is an example.
* **The links themselves.** Give `links`, a table with a source column, an item column and a time column.

## Predict lists

The examples use a database sent as in [Databases](https://docs.tabsutra.com/guides/databases.md), and the `API`, `HEADERS` and `records` set up in the [Quickstart](https://docs.tabsutra.com/quickstart.md).

```python theme={null}
lists = requests.post(f"{API}/v1/databases/{database_id}/recommend", headers=HEADERS, json={
    "source_table": "customers", "dest_table": "products", "link_table": "orders",
    "rows": records(to_score),            # customer_id, date
    "source_column": "customer_id", "time_column": "date", "window_days": 30, "k": 10,
}).json()
```

With links given directly, send them in `links`, and name their item column with `dest_column` and their time column with `link_time_column`.

```python theme={null}
lists = requests.post(f"{API}/v1/databases/{database_id}/recommend", headers=HEADERS, json={
    "source_table": "customers", "dest_table": "products",
    "links": records(purchases), "dest_column": "product_id", "link_time_column": "bought_at",
    "rows": records(to_score), "source_column": "customer_id", "time_column": "date",
    "window_days": 30, "k": 10,
}).json()
```

## How the lists are made

For each source at a date, the model draws on everything the database says about the source and the items: the source's past links, what other sources picked, the other tables that relate the two, and the item columns, including their text. It ranks the items by how likely the link is. Only links and rows dated strictly before the date are read.

When the model finds fewer suitable items than `k`, the list is filled with the items most picked in the window before. Those places are marked `filled_from_popular` and have no probability.

The lists for each date are made separately. The context for a date is made of the lists of the earlier dates, one window apart, whose outcome is already known.

## Evaluate

`/v1/databases/{database_id}/evaluate-recommend` scores the lists of one date with MAP\@k, the measure RelBench uses. By default the date is one window before the last link, so the outcome of every list is known.

```python theme={null}
result = requests.post(f"{API}/v1/databases/{database_id}/evaluate-recommend", headers=HEADERS, json={
    "source_table": "customers", "dest_table": "products", "link_table": "orders", "window_days": 30, "k": 10,
}).json()
print(result["map_at_k"])
```

The result places the model next to rules that need no training:

| Name | The list is |
| - | - |
| `rule_popular` | the items most picked of all time |
| `rule_recent` | the items most picked in the window before the date |
| `rule_past` | the items the source picked before, the latest first |
| `rule_covisit` | the items picked by sources that share items with this one |
| `rule_past_filled`, `rule_covisit_filled` | the same, with short lists filled from the recent ones |

`without_text` shows what the model gets from the item texts.

## Files

`format` can be `json`, `csv` or `parquet`. A table has one row per item, with the columns `id`, `rank`, `item`, `probability` and `filled_from_popular`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.