Feature engineering and preprocessing · insight-0135
Scale sparse matrices without centering them
- Evidence type
- documented fact
- Difficulty
- easy
- Roles
- ML engineer, Data scientist
- Industries
- e-commerce, cross-industry
Summary: situation, action, reason, boundary
Situation
Your features are a sparse matrix, for example one-hot route-by-channel indicators or TF-IDF text features, and you add StandardScaler.
Action
Use MaxAbsScaler or StandardScaler(with_mean=False) for sparse inputs, or skip scaling where the downstream model does not need it.
Reason
Subtracting a mean from a sparse column makes nearly every entry non-zero, destroying the sparsity that makes the data tractable.
Boundary
Linear models, SVMs and k-NN on sparse one-hot or bag-of-words features.
Symptom or risk
Centering turns zeros into non-zeros, exploding memory or crashing the job; with defaults, scikit-learn raises an error instead.
Exceptions
Small sparse matrices can be densified and centered when memory allows and centering helps the solver.
Trade-offs
Unscaled sparse binary features already share a 0-1 range; scaling mainly matters for mixed-magnitude sparse inputs.
Pitfalls
Combining sparse and dense blocks in one ColumnTransformer with a centering scaler applied to everything.
Lessons and concepts
Claims and their sources
Centering sparse data destroys sparsity; MaxAbsScaler is designed for sparse data and StandardScaler accepts sparse input only with with_mean=False.