Text classification
Text feature vectors should be compared by direction rather than total magnitude.
Scikit-Learn · Class
Normalizer rescales each nonzero sample independently to unit l1, l2, or max norm. It supports dense and sparse data and is stateless, so it does not need fitting.
Normalizer rescales each nonzero row independently so its selected norm is one. It is stateless because normalization depends on each sample rather than on statistics learned from the dataset, so fitting is unnecessary for determining the transformation.
class sklearn.preprocessing.Normalizer(*, norm='l2', copy=True)| Name | Type | Description |
|---|---|---|
norm | {'l1', 'l2', 'max'}, default='l2' | Norm used for each nonzero sample: 'l1', 'l2', or 'max'; default is 'l2'. |
copy | bool, default=True | Whether to copy the input; set False to request in-place normalization for compatible NumPy arrays or CSR matrices. |
transform(X, copy=None): scaled X in which each nonzero row has unit norm.
from sklearn.preprocessing import Normalizer
X = [[3.0, 4.0], [0.0, 0.0]]
X_normalized = Normalizer(norm="l2").fit_transform(X)
print(X_normalized)Normalizes each nonzero row independently; the zero row remains zero.
Text feature vectors should be compared by direction rather than total magnitude.
Sparse feature matrices need row-wise unit scaling before a downstream estimator.
A pipeline requires a transformer that normalizes each incoming sample independently.
Normalizer transforms samples row by row, independently of other samples, so each nonzero row receives the requested unit norm. Unlike a feature scaler, it does not estimate dataset-wide parameters.
Follow-up: What does fitting learn? Nothing: the estimator is stateless, so fit is effectively unnecessary and transform can perform the operation directly.
The default is the l2 norm. The norm option also accepts l1 and max; with max, each row is rescaled according to its largest absolute value.
Follow-up: When might max normalization be useful? It bounds each row by its largest absolute component, which can be appropriate when relative magnitudes within a sample matter more than Euclidean length.
The class accepts dense NumPy arrays and scipy sparse matrices. CSR sparse matrices are preferred when avoiding copy or conversion overhead matters.
Follow-up: What does copy=False change? It requests in-place row normalization for compatible NumPy arrays or CSR matrices, so callers must account for input mutation.
normalizeNormalizer