scaling individual samples to have unit norm

Solutions on MaxInterview for scaling individual samples to have unit norm by the best coders in the world

showing results for - "scaling individual samples to have unit norm"
Sixtine
14 Feb 2019
1# scaling individual samples to have unit norm
2
3X = [[ 1., -1.,  2.],
4     [ 2.,  0.,  0.],
5     [ 0.,  1., -1.]]
6X_normalized = preprocessing.normalize(X, norm='l2')
7
8X_normalized
9array([[ 0.40..., -0.40...,  0.81...],
10       [ 1.  ...,  0.  ...,  0.  ...],
11       [ 0.  ...,  0.70..., -0.70...]])
12
13normalizer = preprocessing.Normalizer().fit(X) # fit does nothing
14normalizer
15# Normalizer()
16
17normalizer.transform(X)
18# array([[ 0.40..., -0.40...,  0.81...],
19#        [ 1.  ...,  0.  ...,  0.  ...],
20#        [ 0.  ...,  0.70..., -0.70...]])
21
22normalizer.transform([[-1.,  1., 0.]])
23# array([[-0.70...,  0.70...,  0.  ...]])