> For the complete documentation index, see [llms.txt](https://docs.mediarithmics.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mediarithmics.io/setup-and-monitor/plan-and-integrate/set-up-advanced-features/cohort-based-lookalike.md).

# Cohort-based Lookalike

Configure the cohort model a datamart needs before anyone can build a cohort lookalike segment.

## Your goal

Set up the cohort model once per organisation, so your teams can build [cohort lookalike segments](https://docs.mediarithmics.io/audience-and-activation/build-your-audiences/segment-reference/segment-types/cohort-lookalike-segment.md) from a seed segment.

## Expected outcome

`clustering_cohort` appears on `UserPoint` in the published schema, and a lookalike can be created from a seed segment.

## Before you start

{% hint style="info" %}
Cohort-based Lookalike is not enabled by default. Contact your Account Manager to enable it.
{% endhint %}

The cohort model assigns every user a [cohort](https://docs.mediarithmics.io/audience-and-activation/build-your-audiences/segment-reference/segment-types/cohort-lookalike-segment.md#what-a-cohort-is) from the attributes you chose to characterise your user base. It has to exist on the datamart before anyone can build a cohort lookalike segment, and its attributes are an organisation-level decision rather than a per-segment one.

{% hint style="warning" %}
Only **one** Cohort-based Lookalike model can be set up at a time in an organisation, so agree on the attributes before you create it. Changing them later means rebuilding the model.
{% endhint %}

## Set it up

The steps below have to be performed **in this order**.

{% stepper %}
{% step %}

#### Define the attributes

A cohort is assigned to a user according to the attributes - features, in data-science terms - you have defined to characterise your users. You declare them as JSON.

These attribute types are available:

| Type               | Use it for                                                           | How the values are declared                                                                                        |
| ------------------ | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| `FREQUENCY_ENUM`   | A finite list of values, such as operating systems.                  | `values`: the list itself.                                                                                         |
| `FREQUENCY_NUMBER` | Number buckets, such as age.                                         | `intervals`: `from` inclusive, `to` exclusive. Anything outside the declared intervals falls into an extra bucket. |
| `FREQUENCY_TEXT`   | An infinite or very long list of values, such as keywords or cities. | `vector_size`: used as a modulo on the values, to reduce their disparity to a fixed number. Choose it carefully.   |

`field_path` is the path of the attribute from the `UserPoint` definition in your [schema](https://docs.mediarithmics.io/setup-and-monitor/collect-and-manage-your-data/design-your-data-model-and-schema/schema-definition.md).

Cohorts built on `os_family` (on `UserAgentInfo`, nested in `UserAgent`), `age` (on `UserProfile`) and `city` (on `UserEvent`) are declared like this:

```json
[
  {
    "type": "FREQUENCY_ENUM",
    "field_path": "agents.user_agent_info.os_family",
    "values": [
      "OTHER",
      "WINDOWS",
      "MAC_OS",
      "LINUX",
      "ANDROID",
      "IOS"
    ]
  },
  {
    "type": "FREQUENCY_NUMBER",
    "field_path": "profiles.age",
    "intervals": [
      {
        "from": 0,
        "to": 10
      },
      {
        "from": 10,
        "to": 100
      }
    ]
  },
  {
    "type": "FREQUENCY_TEXT",
    "field_path": "events.city",
    "vector_size": 100
  }
]
```

{% hint style="info" %}
**How the number buckets above read.** First bucket: `age >= 0` and `age < 10`. Second bucket: `age >= 10` and `age < 100`. Third bucket: everything that fell into neither.
{% endhint %}

Recommendations for the selection itself:

* Pick attributes that can be used to segment users and that are relevant to the business.
* Pick attributes available on all your users, logged in and logged out.
* Pick attributes from various typologies: `UserEvent`, `UserProfile`, and so on.
* Select between **3 and 10** attributes, carrying **between 50 and 300 values** in total.
* Keep the default of **1024 cohorts**, which is a `Cohort Id Bit Size` of 10.

The function also needs the GraphQL query that fetches exactly those fields. For the attributes above:

```graphql
{agents {user_agent_info {os_family}} profiles {age} events{city}}
```

{% endstep %}

{% step %}

#### Create the ML Function

Go to **Settings > Datamart > ML Functions** and click **New Ml Function**, pick the datamart the function applies to, then choose the **simhash-cohorts-calulation** algorithm. Fill the configuration panel with:

| Field                           | Value                                                                   |
| ------------------------------- | ----------------------------------------------------------------------- |
| Name                            | `Cohort ML Function`                                                    |
| Hosting Object Type             | `UserPoint`                                                             |
| Field Type Name                 | `ClusteringCohort`                                                      |
| Field Name                      | `clustering_cohort`                                                     |
| Query                           | The GraphQL query from the previous step.                               |
| Properties > Features           | The attributes JSON from the previous step, on one line.                |
| Properties > Cohort Id Bit Size | Sets the number of cohorts in your datamart, as 2^(Cohort Id Bit Size). |

Then save.
{% endstep %}

{% step %}

#### Activate it

Set `batch_mode` to `true` and the status to `ACTIVE`:

```json
PUT https://api.mediarithmics.com/v1/ml_functions/<ml_function_id>
{
  "batch_mode": true,
  "status": "ACTIVE"
}
```

{% endstep %}

{% step %}

#### Update the schema

Make these changes in your runtime schema. Add a `clustering_cohort` field on `UserPoint`:

```graphql
type UserPoint  @TreeIndexRoot(index:"USER_INDEX") {
   ...
   clustering_cohort:ClusteringCohort
   ...
}
```

And create the `ClusteringCohort` type:

```graphql
type ClusteringCohort  {
   id:ID! @TreeIndex(index:"USER_INDEX")
   expiration_ts:Timestamp @TreeIndex(index:"USER_INDEX")
   cohort_id:String! @TreeIndex(index:"USER_INDEX")
   last_modified_ts:Timestamp! @TreeIndex(index:"USER_INDEX")
}
```

See [Schema definition](https://docs.mediarithmics.io/setup-and-monitor/collect-and-manage-your-data/design-your-data-model-and-schema/schema-definition.md) for how a schema change is published.
{% endstep %}

{% step %}

#### Ask for the initial loading

Cohorts on your existing UserPoints are calculated by an initial loading, which the mediarithmics team runs on your datamart. Ask your Account Manager for it once the steps above are done.
{% endstep %}
{% endstepper %}

## Verify

* `clustering_cohort` appears on `UserPoint` in the published schema, and a lookalike can be created from a seed segment.

## Troubleshooting

| Symptom                                                   | Likely cause                                                                                          |
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Cohorts are missing on users who existed before the model | The initial loading has not run. It is triggered by the mediarithmics team: ask your Account Manager. |

## Related pages

<table data-view="cards" data-search="false"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Cohort lookalike segment</strong></td><td>The segment type the cohort model makes possible.</td><td><a href="https://docs.mediarithmics.io/audience-and-activation/build-your-audiences/segment-reference/segment-types/cohort-lookalike-segment.md">Cohort lookalike segment</a></td></tr><tr><td><strong>Expand your audience with Lookalike</strong></td><td>Deciding whether to expand, and judging the result.</td><td><a href="https://docs.mediarithmics.io/audience-and-activation/build-your-audiences/expand-your-audience-with-lookalike.md">Expand your audience with Lookalike</a></td></tr><tr><td><strong>Schema definition</strong></td><td>How a schema change is published.</td><td><a href="https://docs.mediarithmics.io/setup-and-monitor/collect-and-manage-your-data/design-your-data-model-and-schema/schema-definition.md">Schema definition</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mediarithmics.io/setup-and-monitor/plan-and-integrate/set-up-advanced-features/cohort-based-lookalike.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
