Skip to content
ML Academy

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

  1. Situation

    Your features are a sparse matrix, for example one-hot route-by-channel indicators or TF-IDF text features, and you add StandardScaler.

  2. Action

    Use MaxAbsScaler or StandardScaler(with_mean=False) for sparse inputs, or skip scaling where the downstream model does not need it.

  3. Reason

    Subtracting a mean from a sparse column makes nearly every entry non-zero, destroying the sparsity that makes the data tractable.

  4. 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