Skip to main content

StepLog

Take the logarithm of numeric columns.

Usage

Source

StepLog(
    columns,
    *,
    base=math.e,
    offset=0.0,
    signed=False,
    id=None,
)

Each selected column x is replaced by log(x + offset) / log(base).

For a transform that handles skew without any domain restriction, see StepOrderNorm.

A value equal to -offset becomes -inf and anything smaller becomes NaN. These pass through with a warning naming the columns, at both fit and transform time. A positive offset brings zeros into the domain.

signed=True suits columns that span zero. It computes sign(x) * log(|x|) with |x| < 1 flattened to 0, so every real value is valid and the result is continuous through the origin, but flat between -1 and 1. It does not use offset, so passing both is an error.

See Logarithms in Feature Engineering A to Z for background on this technique. See Arguments shared across steps for the arguments this step shares with the others.

Parameters

columns: Selector

Columns to log-transform. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.numeric_features() (see Selectors for the full set), or a narwhals or polars selector (narwhals.selectors.numeric(), polars.selectors.numeric()). Selectors are resolved against the data frame’s schema when the recipe is fitted, so the same selector works on any backend. Only numeric columns are meaningful here.

base: float = math.e

Base of the logarithm; must be positive and not 1. Defaults to math.e (the natural logarithm).

offset: float = 0.0

Added to every value before the logarithm. Defaults to 0.0. Cannot be combined with signed.

signed: bool = False

Whether to use the signed formula described above instead of the plain logarithm. Defaults to False.

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

Examples

Each selected column is replaced by its logarithm:

df = pd.DataFrame({"x": [1.0, 10.0, 100.0, 1000.0]})
df
x
0 1.0
1 10.0
2 100.0
3 1000.0

Specifying the step touches no data:

recipe = Recipe().add(StepLog(s.numeric_features()))
recipe
Recipe (untrained)
  Inputs: no roles assigned
  Steps:
    step_log(select(role='feature', type=NumericType))  [untrained]

There is nothing to estimate, so fit() only resolves the selection:

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

transform() takes the natural logarithm:

fitted.transform(df)
x
0 0.000000
1 2.302585
2 4.605170
3 6.907755

base changes the logarithm’s base, here to the base-10 logarithm:

Recipe().add(StepLog(s.numeric_features(), base=10)).fit(df).transform(df)
x
0 0.0
1 1.0
2 2.0
3 3.0

Zeros are outside the logarithm’s domain:

counts = pd.DataFrame({"x": [0.0, 1.0, 3.0, 7.0]})
counts
x
0 0.0
1 1.0
2 3.0
3 7.0

offset shifts every value up first, which brings them back in:

Recipe().add(StepLog(s.numeric_features(), offset=1.0)).fit(
    counts
).transform(counts)
x
0 0.000000
1 0.693147
2 1.386294
3 2.079442

A column spanning zero has negatives too:

spread = pd.DataFrame({"x": [-100.0, -1.0, 0.0, 1.0, 100.0]})
spread
x
0 -100.0
1 -1.0
2 0.0
3 1.0
4 100.0

signed logs each side separately and flattens the middle:

Recipe().add(StepLog(s.numeric_features(), base=10, signed=True)).fit(
    spread
).transform(spread)
x
0 -2.0
1 -0.0
2 0.0
3 0.0
4 2.0