Principles and implementation of data Normalization (normalization) (Python Sklearn)

Source: Internet
Author: User
Tags normalizer

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.