## Recipe.set_roles()


Return a new recipe with the given role assignments added.


Usage

``` python
Recipe.set_roles(
    *,
    target=None,
    feature=None,
    case_weights=None,
    **custom,
)
```


Standard roles have dedicated keyword arguments; arbitrary custom roles may be passed as additional keywords. Each value is a column name or a list of names. Assignments are *added* to any roles the recipe already carries, so a column may end up with several roles at once; incompatible combinations (e.g. target + feature on one column) are rejected. When the recipe was built with a prototype [data](data.md#mlfeatures.data) frame, every name is checked against it; without a prototype there is nothing to check against, so any name is accepted and typos surface later, when the recipe is fitted.

Roles are what let a step say "all numeric features" without also picking up the target or case weights: selectors such as [Selectors.numeric_features()](Selectors.md#mlfeatures.Selectors.numeric_features) only match columns carrying the `feature` role. Declare `feature` explicitly to keep other columns, such as an ID, out of those selections. A custom role marks columns for your own selectors, or for [update_role_requirements](Recipe.update_role_requirements.md#mlfeatures.Recipe.update_role_requirements).

Roles apply to the columns of the input data, whatever the call's position among the steps. Columns that steps create get their roles from the step that creates them. [set_roles()](Recipe.set_roles.md#mlfeatures.Recipe.set_roles) only adds roles; to add, change, or remove one part way through the pipeline, use [add_role](Recipe.add_role.md#mlfeatures.Recipe.add_role), [update_role](Recipe.update_role.md#mlfeatures.Recipe.update_role) or [remove_role](Recipe.remove_role.md#mlfeatures.Recipe.remove_role). Next to those written before the first step, which also act on the input columns, it keeps the order it was written in, so `update_role("x", new_role="id").set_roles(group="x")` leaves `x` with both roles, while the same two calls the other way round leave it with `"id"` only. A custom role can be selected in a step with [Selectors.has_role()](Selectors.md#mlfeatures.Selectors.has_role).

The `roles` property returns a copy of the roles the recipe starts from: what the constructor and [set_roles()](Recipe.set_roles.md#mlfeatures.Recipe.set_roles) declared, with any role operation written before the first step applied (default features are not shown). Changing the copy does not change the recipe; [summary()](Recipe.summary.md#mlfeatures.Recipe.summary) shows the same roles per column.


## Parameters


`target: str | list[str] | None = None`  
Column(s) to assign the `"target"` role. Default `None`.

`feature: str | list[str] | None = None`  
Column(s) to assign the `"feature"` role. Default `None`.

`case_weights: str | list[str] | None = None`  
Column(s) to assign the `"case_weights"` role. Default `None`.

`**custom: str | list[str]`  
Any further keywords assign arbitrary custom roles, the keyword being the role name (e.g. `group="city"`). The constructor's plural `features=` is rejected here rather than silently becoming a custom role.


## Examples

Assigning roles returns a new recipe, leaving the original untouched:


``` python
recipe = Recipe(data=credit).set_roles(
    target="Status", feature=["Age", "Income", "Home"]
)
recipe
```


    Recipe (untrained)
      Inputs: 14 columns (3 features, 1 target)
      Steps:
        <none>


Custom roles are just extra keywords, and stack with the roles a column already has:


``` python
recipe.set_roles(group="Home").roles.of("Home")
```


    {'feature', 'group'}
