Answers for "scaling individual samples to have unit norm"

0

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.  ...]])
Posted by: Guest on April-16-2020

Python Answers by Framework

Browse Popular Code Answers by Language