# fbt-walkthrough

## Frequently bought together

In this example, we’ll walk through building a ‘Frequently bought together’ feature to show in a mobile e-commerce app.

![Mockup](https://2104209088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9S1KxRBJx1i8W3MPsG%2F-LDqY8Fx3i9xng1RcZbH%2F-LDqY9WS_2p0OkwEymu2%2Ffbt_mockup.PNG?generation=1527776084228479\&alt=media)

When a user views a ‘Simple Plant’ in our e-commerce app, we want to promote a set of products to encourage the user to spend more and hence increase the transaction value.

Therefore, the model that we’d like to build is:

Given a `product listing`, a set of `frequently bought together products` and a `specific user`, predict the `transaction value`.

Let’s say that we’re looking for the following features to train our initial model:

1. The product listing (`product_primary`)
2. Frequently bought together products (`fbt_1`, `fbt_2`, `fbt_3`)
3. Sessions in the past week (`session_index`)
4. Days since install (`days_since_install`)
5. User’s age (`age`)
6. User’s gender (`gender`)
7. User’s country (`country`)
8. User’s spend so far (`spend_to_date`)

and we will predict

1. User's spend in the current session (`transaction_value_this_session`)

Once we’ve trained and deployed the model, we query it with the model features and it returns a prediction for the transaction value in this session.

![Model query overview](https://2104209088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9S1KxRBJx1i8W3MPsG%2F-LDqY8Fx3i9xng1RcZbH%2F-LDqY9WhYo0XiAf7wUwc%2Fquery_overview.png?generation=1527776073581550\&alt=media)

Doing it this way allows us to query the model multiple times with different `fbt_*` products, and use the ones that result in the highest `transaction_value_this_session`.

![Model query option1](https://2104209088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9S1KxRBJx1i8W3MPsG%2F-LDqY8Fx3i9xng1RcZbH%2F-LDqY9WkgX-vli3_GAOS%2Fquery_1.png?generation=1527776084054978\&alt=media)

![Model query option1](https://2104209088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9S1KxRBJx1i8W3MPsG%2F-LDqY8Fx3i9xng1RcZbH%2F-LDqY9WmOMeb_alQH4M5%2Fquery_2.png?generation=1527776084732548\&alt=media)

The model predicts a higher `transaction_value_this_session` for `simple_plant`, `complex_plant` and `plant_food`, so we’ll show that to the user instead of `simple_plant`, `pot` and `watering_can`.

### Raw data

We're assuming that we have the following raw data. These events are based on the Firebase SDK.

Session start event

```javascript
[
{
"event_name": "session_start",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2",
"start_time": "2018-01-14T17:53:57",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"age": "25-34",
"app_Store": "iTunes",
"app_Version": "3.4.2",
"country": "BR",
"device_brand": "Apple",
"device_category": "tablet",
"device_model": "iPad Pro",
"first_open_time": "2017-12-02T23:00:00",
"gender": "male",
"interests": "arts & entertainment, games",
"language": "pt-br",
"new/established": "established",
"os_version": "10.1"
}
]
```

Product page is viewed

```javascript
[
{
"event_name": "view_item",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2",
"item_id": "32",
"item_name": "simple_plant",
"item_category": "plants"
}
]
```

Purchase

```javascript
[
{
"event_name": "ecommerce_purchase",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2"’
"item_id": "32, 42, 94",
"currency": "usd",
"value": "12, 23, 5"
}
]
```

## Transforming data

To train the model, we are looking for data to be organized like this.

| Unique user\_id (appInstanceId) | product\_primary | fbt\_1 | fbt\_2 | fbt\_3 | session\_index | days\_since\_install | age | gender | country | spend\_to\_date | transaction\_value\_this\_session |
| ------------------------------- | ---------------- | ------ | ------ | ------ | -------------- | -------------------- | --- | ------ | ------- | --------------- | --------------------------------- |

Since user may not buy everything in one transaction, `fbt_1`, `fbt_2`, `fbt_3` are the top 3 products bought by the user in a 48 hour window (+- 24 hours) from the `product_primary`

![Anchor](https://2104209088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9S1KxRBJx1i8W3MPsG%2F-LDqY8Fx3i9xng1RcZbH%2F-LDqY9X2ymxgn22hGumz%2Ffbt-anchor.png?generation=1527776089304054\&alt=media)

[Streaming BTS](https://github.com/productml/blurr/tree/1c9e96562bbb9fe68dfd195acd8c60d91a19fac5/docs/examples/frequently-bought-together/fbt-streaming-bts.yml)

[Window BTS](https://github.com/productml/blurr/tree/1c9e96562bbb9fe68dfd195acd8c60d91a19fac5/docs/examples/frequently-bought-together/fbt-window-bts.yml)
