Feature Engineering in Scikit-Learn: A KDnuggets Cheat Sheet

Once feature engineering lives inside a Pipeline, each step is fitted on training data only, and the model is scored what it actually earned. And that is the idea behind this new cheat sheet.



Feature Engineering in Scikit-Learn: A KDnuggets Cheat Sheet

Most of my early scikit-earn mistakes happened around a model rather than inside it. I would scale a column in one notebook cell, encode a category in another, fit the model somewhere further down, and then feel good about a cross-validation score that never survived contact with new data. Nothing was wrong with the estimators. The problem was that my preprocessing had already seen the validation fold before the model ever got there.

What fixed it was not learning more transformers. It was learning where they belong. Once feature engineering lives inside a Pipeline, each step is fitted on training data only, and the model is scored what it actually earned. And that is the idea behind this new cheat sheet: everything on it is something you can drop into that chain.

The pieces that generally get the most use are the boring structural ones. ColumnTransformer is how numeric and categorical columns get their own treatment without me splitting the frame by hand, and make_column_selector means I can pick columns by dtype instead of listing them, so a new column doesn't force me to edit the pipeline. SimpleImputer with add_indicator=True is a good combination to get into the habit of using, because the pattern of what was missing is often signal. On the categorical side, handle_unknown="ignore" in OneHotEncoder has saved me from more prediction-time crashes than I care to admit, and TargetEncoder is my default when cardinality gets high enough that one-hot encoding stops being reasonable.

Two more earn their place for different reasons. set_output(transform="pandas") and get_feature_names_out() are the fastest way to see what your pipeline actually built, which matters when a ColumnTransformer and a PolynomialFeatures step may have turned twelve columns into an even hundred. And the syntax of GridSearchCV is the payoff: once preprocessing is inside the estimator, an imputation strategy becomes a hyperparameter like any other, and you can tune it alongside your regularization strength in one search.

This cheat sheet collects those steps in one place, with the arguments that matter and the ones I keep forgetting.
 
 


Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy


Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy

Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy

No, thanks!