Skip to main content

StepIntercept

Add a constant column with a fixed value.

Usage

Source

StepIntercept(
    *,
    value=1.0,
    name="intercept",
    role="feature",
    id=None,
)

A single constant column is added, by default a column of ones named "intercept".

See Arguments shared across steps for the arguments this step shares with the others.

Parameters

value: float = 1.0

The constant to fill the new column with. Default 1.0. The column’s dtype follows the literal, so an int gives an integer column.

name: str = "intercept"

Name of the new column. Default "intercept". A Recipe rejects the step if the data frame already has a column of this name.

role: str | None = "feature"

Role assigned to the new column (see Roles). Default "feature".

id: str | None = None
Stable step identifier. Defaults to "intercept"; a second step of this type in the same recipe becomes "intercept_2", and passing an id already used in the recipe is an error.

Examples

A constant intercept column of ones is added; the input columns pass through untouched:

df = pd.DataFrame({"x": [1.0, 2.0, 3.0]})
df
x
0 1.0
1 2.0
2 3.0

Specifying the step touches no data:

recipe = Recipe().add(StepIntercept())
recipe
Recipe (untrained)
  Inputs: no roles assigned
  Steps:
    step_intercept()  [untrained]

fit() has nothing to estimate; the step is constant:

fitted = recipe.fit(df)
fitted
Recipe (trained)
  Inputs: 1 columns (1 feature)
  Steps:
    step_intercept(intercept)  [trained]

transform() adds the column:

fitted.transform(df)
x intercept
0 1.0 1.0
1 2.0 1.0
2 3.0 1.0

value and name control what is added:

recipe = Recipe().add(StepIntercept(value=0.0, name="bias"))
fitted = recipe.fit(df)
fitted.transform(df)
x bias
0 1.0 0.0
1 2.0 0.0
2 3.0 0.0