What the simple recipes miss, and how to choose among the alternatives
With a squared loss, the curvature along each direction is set by the variance of the features: the eigenvalues of \(M^\top M / n\), with \(M\) the centered (age, fare) matrix of \(n\) rows.
| raw | 208.62, 2798.59 | ratio 13.4 |
| standardized | 0.9, 1.1 | ratio 1.2 |
A learning rate safe for the steep direction crawls along the flat one.
\(x' = (x^\lambda - 1)/\lambda\), \(\lambda = 0\) means \(\log x\); \(\lambda\) by maximum likelihood. Needs \(x > 0\): refuses the 15 free tickets.
accepts zero and negatives. \(\lambda = -0.098\), close to a log → skewness −0.04.
replaces each value by its rank → −0.94; the 15 free tickets pile up at −5.20.
PowerTransformer standardizes its output by default (shape, then scale). Quantiles keep only the order.| port | intercept | C | Q | S | C + Q + S |
|---|---|---|---|---|---|
| S | 1 | 0 | 0 | 1 | 1 |
| C | 1 | 1 | 0 | 0 | 1 |
| S | 1 | 0 | 0 | 1 | 1 |
| Q | 1 | 0 | 1 | 0 | 1 |
| S | 1 | 0 | 0 | 1 | 1 |
| C | 1 | 1 | 0 | 0 | 1 |
The three port columns always add up to the intercept's column: rank 3 for 4 columns.
The weights are not unique: add 5 to the three port weights, subtract 5 from the intercept, same predictions.
drop='first' removes C, the reference: rank 3 for 3 columns, each coefficient reads "compared with Cherbourg".
drop='first' for linear models with an intercept and no penalty. Keep every column for trees, k-NN (k nearest neighbors) and regularized models: for a distance, "reference" and "unknown" would both be 0 0.Ticket has 681 distinct values for 891 rows: one-hot would create 681 columns of almost only zeros, a recipe for overfitting.
Cabin, the title in Name (Mr, Miss, Mrs, Master...).min_frequency=10 keeps 4 titles and merges the other 13.| encoding of pure noise | train | test |
|---|---|---|
| naive per-category mean | 0.775 | 0.505 |
TargetEncoder (cross-fitted) | 0.496 | 0.505 |
fit_transform encodes each fold with means from the other folds, so no row sees its own label; rare categories are shrunk toward the global mean.fit_transform on train, transform on test. Target-free options: frequency encoding, hashing.KNNImputer, by handMr. James Moran paid £8.46 and has no age. The imputer looks for the \(k = 2\) passengers with the closest fare and averages their ages.
| passenger | age | fare | |fare − 8.46| | |
|---|---|---|---|---|
| 3 | 26 | £7.93 | 0.5 | |
| 1 | 22 | £7.25 | 1.2 | |
| 4 | 35 | £53.10 | 44.6 | |
| 2 | 38 | £71.28 | 62.8 |
KNNImputeraverage of the \(k\) most similar rows. On class, siblings and parents: std 13.29, no impossible age.
IterativeImputera regression of each column on the others, cycled (MICE, multivariate imputation by chained equations). It produced 7 negative ages: it does not know that age is positive.
Per column: missing values → shape → scale, every step fitted on the training rows. Click the answers to build a recipe.
| model family | scaling | shape | encoding | missing values |
|---|---|---|---|---|
| k-NN, k-means, SVM, PCA | required | helps | one-hot | must be imputed |
| linear/logistic regression, neural networks | required | helps | one-hot, drop='first' if no penalty | must be imputed |
| trees, random forests, gradient boosting | not needed | not needed | ordinal codes are fine | scikit-learn's trees accept NaN |
SVM: support vector machine; PCA: principal component analysis; NaN: not a number, a missing value.
drop='first' only for linear models with an intercept and no penalty.ColumnTransformer: 12 columns, no NaN, no text.Pace: step 7 in 5 min; step 8 and the first simulation in 10 min; step 9 and the second in 10 min.