principle
Data Normalization (normalization) is a vector that transforms each sample (vector) of data into a unit norm, each of which is independent of each other. In effect, each component value in the vector is divided by the normalization factor. Common regularization factors are L1, L2, and Max. Suppose, for a vector of length n, the formula of its regularization factor Z, as follows:
Note: Max is different from infinity norm in that the infinity norm needs to take the absolute value of all the components of the vector first, then the largest of them, and Max is the maximum component value in the vector, and no absolute action is required.
Added: The first-order norm is also known as the Manhattan Distance (Manhanttan distance) or block distance; the second-order norm is also known as the Euclidean distance (Euclidean distance).
Implement
In the Python library Sklearn, there are two implementations of data normalization, both of which can be norm by selecting a regularization factor, with options of ' L1 ', ' L2 ' and ' Max '.
Method One: Using the Sklearn.preprocessing.Normalizer class, the sample code is as follows:
# !/usr/bin/env python # -*-Coding:utf8-*- # Author:klchang
# Use Sklearn.preprocessing.Normalizer class to normalize data.
from __future__ Importprint_functionImportNumPy as NP fromSklearn.preprocessingImportNormalizerx= Np.array ([1, 2, 3, 4], dtype='float32'). Reshape (1,-1)Print("before normalization:", x) options= ['L1','L2','Max'] forOptinchoptions:norm_x= Normalizer (norm=opt). Fit_transform (x)Print("After %s normalization:"% opt.capitalize (), norm_x)
Method Two: Using the Sklearn.preprocessing.normalize function, the sample code is as follows:
#!/usr/bin/env python#-*-Coding:utf8-*-#Author:klchang
# Use Sklearn.preprocessing.normalize function to normalize data.
from __future__ Importprint_functionImportNumPy as NP fromSklearn.preprocessingImportNormalizex= Np.array ([1, 2, 3, 4], dtype='float32'). Reshape (1,-1)Print("before normalization:", x) options= ['L1','L2','Max'] forOptinchoptions:norm_x= Normalize (x, norm=opt)Print("After %s normalization:"% opt.capitalize (), norm_x)
References
1. Scikit-learn Normalization mode (L1 vs L2 & Max). Https://stats.stackexchange.com/questions/225564/scikit-learn-normalization-mode-l1-vs-l2-max
2. Sklearn.preprocessing.normalizer-scikit-learn documentation. Http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Normalizer.html
3. Sklearn.preprocessing.normalize-scikit-learn documentation. Http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.normalize.html
4. Scikit-learn Documentation-4.3. preprocessing data. Http://scikit-learn.org/stable/modules/preprocessing.html
5. Norm (mathematics). Https://en.wikipedia.org/w/index.php?title=Norm_ (mathematics) &oldid=838245314
Principles and implementation of data Normalization (normalization) (Python Sklearn)