> For the complete documentation index, see [llms.txt](https://docs.sdv.dev/sdmetrics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sdv.dev/sdmetrics/data-metrics/diagnostic/categoryadherence.md).

# CategoryAdherence

This metric measures whether a synthetic column adheres to the same category values as the real data. (The synthetic data should not be inventing new category values that are not originally present in the real data.)

## Data Compatibility

* **Categorical:** This metric is meant for discrete, categorical data
* **Boolean:** This metric is meant for boolean data

*If you have missing values in the real data, then the metric will consider them valid in the synthetic data. Otherwise, they will be marked as an invalid category. All types of missing values (NaN, None, etc. will be counted as the same category of 'missing'.)*

## Score

* **(best) 1.0**: All category values in the synthetic data were present in the real data
* **(worst) 0.0**: None of the category values in the synthetic data were present in the real data

Any score in between tells us the proportion of data points that are adhering to the correct values. For example, 0.6 means that 60% of synthetic data points have a value present in in the real data. Meanwhile, the remaining 40% contain new values that were never present in the real data.

## How does it work?

This metric extracts the set of unique categories, that are present in the real column, *Cr*.&#x20;

Then it finds the of data points of the synthetic data, *s*, that are found in the set *C*. The score is the proportion of these data points as compared to all the synthetic data points.

$$
score = \frac{| s, s \in C\_r |}{|s|}
$$

## **Usage**

{% hint style="success" %}
**Recommended Usage:** The [Diagnostic Report ](/sdmetrics/data-metrics/diagnostic/diagnostic-report-api.md)applies this metric to applicable columns.
{% endhint %}

To manually apply this metric, access the `single_column` module and use the `compute` method.

```python
from sdmetrics.single_column import CategoryAdherence

CategoryAdherence.compute(
    real_data=real_table['column_name'],
    synthetic_data=synthetic_table['column_name']
)
```

**Parameters**

* (required) `real_data`: A pandas.Series object with the column of real data
* (required) `synthetic_data`: A pandas.Series object with the column of synthetic data&#x20;

## **FAQs**

<details>

<summary>Is there an equivalent metric for continuous data?</summary>

For continuous datasets, many values are possible. Use the [BoundaryAdherence](/sdmetrics/data-metrics/diagnostic/boundaryadherence.md) metric to ensure they within the correct min/max bounds.

</details>

<details>

<summary>Does this metric measure quality or data coverage? </summary>

No. This metric is a measure of validity, as we generally consider discrete data to be *valid* only if it contains the correct category values.

* **Data quality** refers to the frequency of each particular category value. To compare this, use the [TVComplement](/sdmetrics/data-metrics/quality/tvcomplement.md) metric.
* **Data coverage** refers to the idea that the synthetic data should cover at least 1 of each category value. To measure this, use the [CategoryCoverage](/sdmetrics/data-metrics/quality/categorycoverage.md) metric.

</details>


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sdv.dev/sdmetrics/data-metrics/diagnostic/categoryadherence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
