Pairwise Product and Ratio Features

Quick Overview

Implement a pandas transformer that appends product and ratio features for chosen column pairs while keeping every output value finite. It tests train-time median imputation, safe division by zero, overflow handling, and stable feature naming and column order.

Pairwise Product and Ratio Features

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `PairwiseInteractionFeatures(pairs, fill_value=0.0)`. For each `(a, b)` in `pairs`, `transform(X)` returns a copy of the DataFrame with columns `a * b` and `a / b` appended. Every output value must be finite. The transformer must meet these requirements: - `fit` stores `medians_[col]` (the median of finite values, `0.0` if there are none) for each referenced column. `transform` replaces `NaN` and `±inf` inputs with these medians. - A zero denominator, or any non-finite product or ratio (for example from overflow), becomes `fill_value`. - New columns are named `{a}_x_{b}` and then `{a}_div_{b}`, in pair order, after the original columns. Their dtype is `float64`. `get_feature_names_out()` returns this list. - The input is not mutated and the index is preserved. - A referenced column missing in `fit` or `transform` raises `KeyError`. Calling `transform` before `fit` raises `RuntimeError`. Example: ```python X = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2.0, 0.0, 4.0, np.nan]}) PairwiseInteractionFeatures([("a", "b")]).fit_transform(X) # medians_: a=2.5, b=2.0 (NaN in b -> 2.0) # a_x_b = [2.0, 0.0, 12.0, 8.0] # a_div_b = [0.5, 0.0, 0.75, 2.0] # b == 0 -> fill_value ``` ```hint Order of operations Decide what happens to a `NaN` or infinite input before it reaches the multiply or the divide, and which statistics you are allowed to use for that at transform time. ``` ```hint Finite inputs can still fail Look at the `out=` and `where=` arguments of `np.divide`. Then ask what `1e200 * 1e200` returns. ``` ### What a Strong Answer Covers - How medians are learned in `fit` and applied in `transform`, including a column with no finite training values. - How zero denominators and overflow are kept from producing non-finite outputs. - Column naming, order and dtype, and how `get_feature_names_out()` stays in sync with the output. - Input immutability, index preservation and the two error cases. - The time and memory cost as the number of pairs grows. ### Follow-up Questions - How would you protect ratios against near-zero denominators, not just exact zeros? - How would you scale this to all pairs among hundreds of columns? - Would log-ratios ever be preferable to plain ratios?

Overview: Implement a pandas transformer that appends product and ratio features for chosen column pairs while keeping every output value finite. It tests train-time median imputation, safe division by zero, overflow handling, and stable feature naming and column order.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement PairwiseInteractionFeatures(pairs, fill_value=0.0). For each (a, b) in pairs, transform(X) returns a copy of the DataFrame with columns a * b and a / b appended. Every output value must be finite.

The transformer must meet these requirements:

  • fit stores medians_[col] (the median of finite values, 0.0 if there are none) for each referenced column. transform replaces NaN and ±inf inputs with these medians.
  • A zero denominator, or any non-finite product or ratio (for example from overflow), becomes fill_value .
  • New columns are named {a}_x_{b} and then {a}_div_{b} , in pair order, after the original columns. Their dtype is float64 . get_feature_names_out() returns this list.
  • The input is not mutated and the index is preserved.
  • A referenced column missing in fit or transform raises KeyError . Calling transform before fit raises RuntimeError .

Example:

X = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2.0, 0.0, 4.0, np.nan]})
PairwiseInteractionFeatures([("a", "b")]).fit_transform(X)
# medians_: a=2.5, b=2.0 (NaN in b -> 2.0)
# a_x_b   = [2.0, 0.0, 12.0, 8.0]
# a_div_b = [0.5, 0.0, 0.75, 2.0]   # b == 0 -> fill_value

What a Strong Answer Covers Guidance

  • How medians are learned in fit and applied in transform , including a column with no finite training values.
  • How zero denominators and overflow are kept from producing non-finite outputs.
  • Column naming, order and dtype, and how get_feature_names_out() stays in sync with the output.
  • Input immutability, index preservation and the two error cases.
  • The time and memory cost as the number of pairs grows.

Follow-up Questions Guidance

  • How would you protect ratios against near-zero denominators, not just exact zeros?
  • How would you scale this to all pairs among hundreds of columns?
  • Would log-ratios ever be preferable to plain ratios?