Feature Transformation (Logistic)
π― Purpose
Use this card to decide when to apply transformations when working with logistic regression, Poisson, or negative binomial models. Focuses on skew handling, scale sensitivity, and model assumptions.
π 1. Trigger Logic by Feature Type
πΉ Numeric (Continuous)
Condition |
Transformation |
|
|
Right-skewed distribution ( |
skew |
> 1) |
β
Log or Sqrt Transform |
Wide magnitude spread |
β
StandardScaler or MinMaxScaler (esp. for regularized logistic) |
|
|
Count-based exposure (e.g., minutes , visits ) |
β
Log or Offset transform (Poisson/Negative Binomial) |
|
|
πΉ Categorical
Condition |
Transformation |
Nominal (unordered) |
β
One-hot Encoding |
Ordinal (known rank) |
β
Ordinal Encoding |
High-cardinality |
π Grouping, embedding, or collapse (manual) |
π§ͺ 2. Model-Specific Cues
Model |
Trigger |
Action |
Logistic Regression |
Skewed numeric, regularization |
Scale or log |
Poisson Regression |
Log-linear fit assumption |
Log transform predictors + offset(optional) |
Negative Binomial |
Same as Poisson, handles overdispersion |
Same transforms apply |
# Standard Scaling
from sklearn.preprocessing import StandardScaler
X_scaled = StandardScaler().fit_transform(X)
# Log transform for positive skewed count data
X['log_visits'] = np.log1p(X['visits'])
# One-hot encoding for categorical
X = pd.get_dummies(X, columns=['device_type'])
# Offset for Poisson
model = sm.GLM(y, X, family=sm.families.Poisson(), offset=np.log(X['exposure']))
β
Checklist Before Modeling
- [ ] Numeric features reviewed for skew or count-like structure
- [ ] Categorical variables encoded correctly (one-hot or ordinal)
- [ ] For Poisson/NB: offset column included (log of exposure)
- [ ] Scaled if using regularized logistic (L1/L2)
- [ ] Transforms documented and justified in EDA or notebook
π‘ Tip
βWhen modeling probabilities or counts, log is your best friend β but only when used intentionally.β