Degree-2 Pairwise Feature Transformer
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `PairwiseFeatures`, a transformer that takes a pandas `DataFrame` and appends all degree-2 products of its columns. With `interaction_only=True`, squared terms are omitted. Output is a `float64` `DataFrame` with descriptive column names.
The class must meet these requirements:
- Output columns are the original columns, then for each `i` in fit order, the pairs `(i, j)` with `j ≥ i` (`j > i` if `interaction_only`), named `a^2` or `a*b` (sklearn order).
- `transform` selects the fitted columns in fit order, ignores extra columns, and preserves the input index. Never mutate the input. NaN propagates.
- Non-`DataFrame` input raises `TypeError`. A fitted column missing at transform time raises `ValueError`.
- Calling `transform` or `get_feature_names_out` before `fit` raises `RuntimeError`.
- `fit` returns `self`. `get_feature_names_out()` returns a `list` of names.
Example:
```python
X = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, -1]})
PairwiseFeatures().fit_transform(X)
# a b a^2 a*b b^2
# 0 1.0 4.0 1.0 4.0 16.0
# 1 2.0 5.0 4.0 10.0 25.0
# 2 3.0 -1.0 9.0 -3.0 1.0
PairwiseFeatures(interaction_only=True).fit(
pd.DataFrame({"x": [1.0], "y": [2.0], "z": [3.0]})
).get_feature_names_out()
# ['x', 'y', 'z', 'x*y', 'x*z', 'y*z']
```
```hint Work out the layout in fit
The column order, the pairs to multiply and their names depend only on the training columns. Decide what to store at fit time so that `transform` only has to select columns and multiply.
```
```hint Protect the caller's frame
Integer input must come out as `float64`, and the caller's `DataFrame` must be unchanged afterwards. Check whether the array you multiply is a copy or a view of the input.
```
### Constraints & Assumptions
- No bias column and no degree above 2.
- Do not use `sklearn.preprocessing.PolynomialFeatures`.
- Every fitted column holds values that can be cast to `float64`.
### What a Strong Answer Covers
- Building the column order, the index pairs and the names once in `fit`, in the order sklearn uses for degree 2.
- Aligning a transform-time frame by the fitted names: reordering, dropping extra columns, and raising `ValueError` on missing ones.
- A `float64` result that keeps the input index, leaves the input untouched and lets NaN propagate.
- The full error contract (`TypeError`, `ValueError`, `RuntimeError`) and `fit` returning `self`.
- How the output width and the cost grow with the number of input columns.
### Follow-up Questions
- How would you extend this to any degree, and how fast does the number of output columns grow?
- With 1,000 input columns the output has about half a million columns. What would you do about memory, and what changes if the input is sparse?
- Should features be scaled before the products are built, and how does that affect a regularized linear model trained on them?
- What would you add so this class works inside an sklearn `Pipeline` or `ColumnTransformer`?
Overview: Implement a pandas transformer that appends every degree-2 product of a DataFrame's columns, with an option to drop the squared terms. It tests sklearn-style column ordering and naming, alignment between fit and transform, float64 output that leaves the input unchanged, and a clear error contract.