v Normalizing Observations - Machine Learning

Normalizing Observations

Preliminaries

# Load libraries
from sklearn.preprocessing import Normalizer
import numpy as np

Create Feature Matrix

# Create feature matrix
X = np.array([[0.5, 0.5], 
              [1.1, 3.4], 
              [1.5, 20.2], 
              [1.63, 34.4], 
              [10.9, 3.3]])

Normalize Observations

Normalizer rescales the values on individual observations to have unit norm (the sum of their lengths is one).

# Create normalizer
normalizer = Normalizer(norm='l2')

# Transform feature matrix
normalizer.transform(X)
array([[ 0.70710678,  0.70710678],
       [ 0.30782029,  0.95144452],
       [ 0.07405353,  0.99725427],
       [ 0.04733062,  0.99887928],
       [ 0.95709822,  0.28976368]])