> For the complete documentation index, see [llms.txt](https://docs.sdv.dev/sdv/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/sdv/modeling/constraint-augmented-generation-cag/sandbox-constraints/multiselectquestion.md).

# ❖ MultiSelectQuestion

{% hint style="warning" %}
**❖ Sandbox Constraint.** This constraint is still in an experimental state. We are still collecting feedback about whether it works accurately and meets the desired goals. The API parameters and functionality may change as we improve the constraint.

Sandbox constraints are available to SDV Enterprise customers who have purchased the CAG Bundle, an optional add-on to SDV Enterprise. For more information, please visit the [CAG Bundle](/sdv/explore/sdv-bundles/cag.md) page.
{% endhint %}

Use the **MultiSelectQuestion** constraint when you have survey data that records the responses to a multi-select question. This constraint expects that your exported data contains one column per multi-select option.

<figure><img src="/files/jXtKb1ePqpo4EBgxHdiw" alt=""><figcaption><p>In this survey question #18 (<code>Q18</code>) is a multi-select question. Each option corresponds to a different column. For example <code>Q18_1</code> denotes whether the responser selected the first option; <code>Q18_2</code> denotes whether they selected the second option; and so on.</p></figcaption></figure>

This constraint is designed to emulate survey responses for a multi-select question. Configure it with the same options as your survey in order to see high quality synthetic data. Configuration options include:

* **Max number of options allowed**, for example if you are asking survey responders to "select up to 3 choices" based on their preferences. Each response should only contain up to the max number of allowable options selected.
* **An exclusive option**, such as "none of the above" or "other". When selected, all of the other options are unselected.

## Constraint API

Create a `MultiSelectQuestion` constraint.

**Parameters**:&#x20;

* (required) `column_names`: A list of strings that describe all the columns corresponding to the multi-select question. Each column should represent a different possible option to the multi-select question. The sdtype of these columns should be numerical, categorical, or boolean.
* `table_name`: The name of the table that contains the columns. *This is required if you have multi-table data.*
* `max_num_selected`: An integer > 0 representing the maximum number of options that a responder is allowed to select.
  * (default) `None`: There is no maximum, meaning that it is possible for responders to select all of the options (except the exclusive column, if one exists).
* `exclusive_column_name`: A string with the name of the column that represents the exclusive option, such as "none of the above" or "other". If the exclusive option is selected, than all the other options are unselected.
  * (default) `None`: There is no exclusive column for this multi-select question.

```python
from sdv.cag.sandbox import MultiSelectQuestion

my_constraint = MultiSelectQuestion(
    column_names=['Q18_1', 'Q18_2', 'Q18_3', 'Q18_4', 'Q18_5', 'Q18_6'],
    max_num_selected=3,
    exclusive_column_name='Q18_6'
)
```

Make sure that all the tables and columns you provide are in your [Metadata](/sdv/integration/metadata.md).

### Usage

Apply the constraint to any SDV synthesizer. Then fit and sample as usual.

```python
synthesizer = GaussianCopulaSynthesizer(metadata)
synthesizer.add_constraints([my_constraint])

synthesizer.fit(data)
synthetic_data = synthesizer.sample(num_rows=10)
```

For more information about using predefined constraints, please see the [**Constraint-Augmented Generation tutorial**](https://colab.research.google.com/drive/1WCMQujfVKL5giULZXOPPIBoMfzR9Zj68?usp=sharing).

## FAQs

<details>

<summary>What kind of data is compatible with the constraint?</summary>

This constraint expects that your exported data contains one column per multi-select option. This constraint is able to automatically parse many different types of multi-select response encodings. Below are some examples of the different types of encodings.

<table><thead><tr><th width="186.28125">Export Type</th><th>Encoding for Selected Values</th><th>Encoding for Unselected Values</th></tr></thead><tbody><tr><td>Binary indicator</td><td><code>1</code> or <code>True</code></td><td><code>0</code> or <code>False</code></td></tr><tr><td>Sparse indicators</td><td><code>1</code></td><td><code>null</code></td></tr><tr><td>Label export</td><td>Label of the choice (eg. <code>"chocolate"</code>, <code>"vanilla"</code>, ...)</td><td><code>null</code></td></tr><tr><td>Recode values</td><td>Different numerical code per column (eg. <code>1</code>, <code>2</code>, ...)</td><td><code>null</code></td></tr><tr><td>Three-state encoding</td><td><code>1</code>, label, or recode value</td><td><code>0</code> for explicitly unselected<br><code>null</code> for skipped</td></tr></tbody></table>

This is why the sdtypes of your columns may vary between categorical, boolean, or numerical. All of the options that correspond to the survey question should have the same type of encoding and the same sdtype.

</details>

<details>

<summary>When should I add a MultiSelectQuestion constraint?</summary>

This constraint is explicitly designed to create realistic synthetic data for multi choice questions with configuration options such as the max number of selections and exclusive options.

If you do not have any of these configurations in your multi-select question, then there is no need to add the constraint. But adding the constraint anyway should not adversely affect your synthetic data quality.

</details>

<details>

<summary>How does this constraint handle unselected and skipped options?</summary>

This constraint learns which values correspond to selected options, unselected options, and skipped options. In many cases unselected and skipped options are encoded the same way, with a NaN or None value. However, a 3-state encoding might make a distinction. For example, if an option is shown to the respondent but not selected by them, it is marked as a `0`; if it is not shown at all, then it is marked as a `NaN`.&#x20;

This constraint currently allows skipping the entire question but not skipping particular options.

In case of confusion, you can override the selected and unselected values that should be used for each column. Provide a dictionary that maps each column name to the selected or unselected value.

```python
my_constraint.selected_value_override = {
  'Q18_5': 5
}

my_constraint.unselected_value_override = {
  'Q18_5': np.nan
}
```

</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/sdv/modeling/constraint-augmented-generation-cag/sandbox-constraints/multiselectquestion.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.
