Sub topic 1
What Feature Engineering Is and Why It Matters
Prerequisites
Assume you know basic Python and pandas, features and labels, train/test splits, and scikit-learn’s fit and predict workflow. You should also know that a model learns patterns from the columns it receives.
What Feature Engineering Is and Why It Matters
Feature engineering is the process of turning raw data into useful input variables, called features. A feature can be an existing column, a cleaned value, or a new value derived from several columns.
A model cannot automatically understand that a timestamp contains a weekday or that a missing value may be meaningful. Good features expose patterns in a form the model can learn. Feature engineering can therefore affect quality as much as the choice of algorithm.
Know Your Columns: Feature Types and What They Imply
Start by identifying each column’s meaning, not just its storage type.
| Feature type | Example | Common treatment |
|---|---|---|
| Numeric | `income`, `age` | Impute, scale, or transform |
| Categorical | `city`, `plan` | One-hot or ordinal encoding |
| Date-time | `signup_time` | Extract calendar parts |
| Text | `review` | Convert words to numeric features |
Numeric columns may need scaling or a log transform. Categories need a numeric representation. Date-time columns often become features such as hour, month, or “is weekend.” Text commonly becomes word counts or weighted word scores.
Data Leakage: The Rule That Shapes Everything Else
Data leakage occurs when training uses information that would be unavailable when making a real prediction. It produces scores that look strong but fail in production.
For example, calculating an imputation median or category mapping from the complete dataset allows validation rows to influence training. Select statistics and encodings using only the training portion. Put transformations inside a scikit-learn Pipeline so cross-validation refits them separately for each training fold.
Handling Missing Values
Missingness can come from failed collection, an inapplicable field, or a meaningful absence. First measure where values are missing and ask what the absence means.
For numeric data, common choices are median imputation or a model-based method. For categorical data, use the most frequent category or an explicit "Unknown" category. Add a missing-value indicator when the fact that a value is absent may predict the label. Fit every imputer on training data only.
Exercise
Why can filling missing values before a train/test split leak information?
Show answer
The fill value is calculated using test rows, so the training process indirectly sees information from the evaluation set.