Tests scaling, skew transforms, polynomial and binned features, categorical and target encoding, cyclical datetime features and keeping every fitted step leakage-safe, mostly by computing the values by hand.
Based on chapter 14. Every answer comes with an explanation and a link back to the section.
/Q14
Quiz
Question 1 of 25
Scaling numeric features Level 1
Progress
A customer's features include income (tens of thousands) and age (tens). For which model can you skip feature scaling?
/02
练习From the book
7 exercises
The chapter's own exercises are open questions. Try one, then compare with the model answer.
Exercise 14.1 [W]
Explain why tree-based models don't need feature scaling while linear and distance-based models do, using the "splits on thresholds, not magnitudes" framing from this chapter.
Show the model answer
Tree-based models make decisions by splitting on a threshold ("is age > 35?"), and the split location adapts to whatever scale the data is already in. Multiplying a feature by 1000 just shifts where the threshold lands, without changing which points fall on which side. Linear and distance-based models combine features directly instead: weighted sums, Euclidean distances. A feature on a much larger scale mechanically dominates the combination or distance calculation, regardless of its actual predictive value, unless scaled to a comparable range first.
Exercise 14.2 [W]
For each, name the correct encoding from this chapter: (a) a size column with values small/medium/large; (b) a product_sku column with 50,000 unique values feeding a linear model; (c) a country column with 15 unique values, no natural order.
Show the model answer
(a) Ordinal encoding: small/medium/large has a genuine order. (b) Target or frequency encoding (CV-safe): 50,000 unique values feeding a linear model rules out one-hot (too many columns) and label encoding (implies a false order the linear model would misread). (c) One-hot encoding: 15 unique values, no natural order, is squarely in one-hot's low-cardinality nominal range.
Exercise 14.3 [A]
Using Listing 14.6, compute hour_sin/hour_cos for hour 23 and hour 0 by hand, and show numerically that their Euclidean distance is small, in contrast to the raw hour values, which differ by 23.
Show the model answer
At hour 23, hour_sin(23)=sin(2π⋅23/24)≈−0.259, hour_cos(23)=cos(2π⋅23/24)≈0.966. hour_sin(0)=0, hour_cos(0)=1. Euclidean distance ≈(−0.259−0)2+(0.966−1)2≈0.067+0.001≈0.26, small, correctly reflecting that hour 23 and hour 0 are only one hour apart, versus the raw integer difference of 23.
Exercise 14.4 [A]
Using Listing 14.8's pattern, explain what result you would expect if customer_id were replaced with a column that directly encoded a noisy version of the label (e.g. last_order_value_rounded), would the leaky-vs-safe CV R2 gap this chapter measured (0.872 vs. 0.870) likely grow, shrink, or stay about the same?
Show the model answer
Replacing customer_id with a column that directly (if noisily) encodes the label would very likely make the leaky-vs-safe gap grow substantially larger than 0.872 vs. 0.870. Per the Worked Example's explanation, leakage's damage scales with how strongly the leaking column correlates with the target once leaked. A column that's essentially a disguised version of the label is about as strongly correlated as a feature can be. Naive (leaky) encoding would likely produce a dramatically inflated CV score that collapses on the true held-out test set, unlike the mild, honest gap measured for the weakly-predictive customer_id.
Exercise 14.5 [E]
A teammate wants to one-hot encode a product_id column with 8,000 unique values "to keep things simple." Using this chapter's cardinality guidance, write the two or three sentences you'd say instead.
Show the model answer
"8,000 one-hot columns will blow up training time and memory for very little benefit. Most product IDs will appear too rarely for the model to learn anything reliable about that specific dummy column, and you'll be feeding the model 8,000 mostly-empty columns instead of a compact signal. Frequency encoding or CV-safe target encoding captures the useful signal in one column instead."
Exercise 14.6 [E]
This chapter's measured leaky-vs-safe gap was small (0.872 vs. 0.870). Using the Worked Example's explanation for why, describe a realistic feature (not customer_id) in the same order dataset that would very likely produce a much larger leakage gap if encoded naively.
Show the model answer
A column like last_order_value_rounded (or any feature that's derived from, or closely tracks, the order value itself after the fact) would be a strong candidate. Say such a column existed and were naively target-encoded, or even just included as a raw feature, without checking whether it's available before the order value is known. It would very likely produce a large, disqualifying leaky-vs-safe gap, since it's effectively leaking the label back into the features.
Exercise 14.7 [R]
Read the scikit-learn TargetEncoder documentation (or category_encoders.TargetEncoder). Identify one detail of its built-in cross-fitting behaviour that differs from Procedure 14.2's hand-written version, and explain what problem that difference solves.
Show the model answer
Discussion only. sklearn's built-in TargetEncoder (added in recent sklearn versions) performs its cross-fitting internally and automatically during .fit_transform(). It chooses fold splits and smoothing parameters without the user hand-writing the loop. This removes the risk of a manual implementation bug, such as an off-by-one in fold indexing, that could silently reintroduce leakage in a hand-rolled version like Procedure 14.2's. One trap remains. On training data, call .fit_transform(), which cross-fits. Calling .fit() then .transform() on the same training data skips the cross-fitting and leaks.