How are standardscaler sklearn different?

245    Asked by AndreaBailey in SQL Server , Asked on Nov 16, 2022

What is the difference between standard scaler and normalizer in sklearn.preprocessing module? Don't both do the same thing? i.e remove mean and scale using deviation?

Answered by Amit Sinha

Your question - How are standardscaler sklearn different can be answered as - Normalizer Normalize samples individually to unit norm. Each sample (i.e. each row of the info matrix) with a minimum of one non zero element is rescaled severally of alternative samples in order that its norm (l1 or l2) equals one.

This transformer is able to figure each with dense numpy arrays and scipy.sparse matrix (use CSR format if you would like to avoid the burden of a replica / conversion). Scaling inputs to unit norms may be a common operation for text classification or cluster as an example. For instance the real number of two l2-normalized TF-IDF vectors is the cosine similarity of the vectors and is the base similarity metric for the Vector space Model normally used by the Information Retrieval community.

from sklearn.preprocessing import Normalizer

X = [[4, 1, 2, 2],[1, 3, 9, 3],[5, 7, 5, 1]]
transformer = Normalizer().fit(X) # fit does nothing.
Normalizer(copy=True, norm='l2')
Standardise options by removing the mean and scaling to unit variance
The standard score of sample x is calculated as:
z = (x - u) / s

where u is that the mean of the training samples or zero if with_mean=False and s is that the variance of the training samples or one if with_std=False. Standardization of a dataset could be a common demand for several machine learning estimators: they may behave badly if the individual options don't seem like standard normally distributed data (e.g. Gaussian with 0 mean and unit variance).

from sklearn.preprocessing import StandardScaler

data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
StandardScaler(copy=True, with_mean=True, with_std=True)
print(scaler.transform([[2, 2]]))
 Note : The main difference is that Standard Scalar is applied on Columns, while Normalizer is applied on rows, So make sure you reshape your data before normalising it.
 Note : what is sklearn normalise?

Class sklearn.preprocessing. Normalizer (norm='l2', *, copy=True)[source] Normalise samples individually to unit norm. Each sample (i.e. each row of the data matrix) with at least one non zero component is rescaled independently of other samples so that its norm (l1, l2 or inf) equals one.

Your Answer


Parent Categories