standardization machine learning code example

Example: scaling individual samples to have unit norm

# scaling individual samples to have unit norm

X = [[ 1., -1.,  2.],
     [ 2.,  0.,  0.],
     [ 0.,  1., -1.]]
X_normalized = preprocessing.normalize(X, norm='l2')

X_normalized
array([[ 0.40..., -0.40...,  0.81...],
       [ 1.  ...,  0.  ...,  0.  ...],
       [ 0.  ...,  0.70..., -0.70...]])

normalizer = preprocessing.Normalizer().fit(X) # fit does nothing
normalizer
# Normalizer()

normalizer.transform(X)
# array([[ 0.40..., -0.40...,  0.81...],
#        [ 1.  ...,  0.  ...,  0.  ...],
#        [ 0.  ...,  0.70..., -0.70...]])

normalizer.transform([[-1.,  1., 0.]])
# array([[-0.70...,  0.70...,  0.  ...]])