I have the following numpy array:
foo = np.array([[0.0, 10.0], [0.13216, 12.11837], [0.25379, 42.05027], [0.30874, 13.11784]])
which yields:
[[  0.       10.     ]
 [  0.13216  12.11837]
 [  0.25379  42.05027]
 [  0.30874  13.11784]]
How can I normalize the Y component of this array. So it gives me something like:
[[  0.       0.   ]
 [  0.13216  0.06 ]
 [  0.25379  1    ]
 [  0.30874  0.097]]
                Referring to this Cross Validated Link, How to normalize data to 0-1 range?, it looks like you can perform min-max normalisation on the last column of foo. 
v = foo[:, 1]   # foo[:, -1] for the last column
foo[:, 1] = (v - v.min()) / (v.max() - v.min())
foo
array([[ 0.        ,  0.        ],
       [ 0.13216   ,  0.06609523],
       [ 0.25379   ,  1.        ],
       [ 0.30874   ,  0.09727968]])
Another option for performing normalisation (as suggested by OP) is using sklearn.preprocessing.normalize, which yields slightly different results - 
from sklearn.preprocessing import normalize
foo[:, [-1]] = normalize(foo[:, -1, None], norm='max', axis=0)
foo
array([[ 0.        ,  0.2378106 ],
       [ 0.13216   ,  0.28818769],
       [ 0.25379   ,  1.        ],
       [ 0.30874   ,  0.31195614]])
                        I think you want this:
foo[:,1] = (foo[:,1] - foo[:,1].min()) / (foo[:,1].max() - foo[:,1].min())
                        You are trying to min-max scale between 0 and 1 only the second column.
Using sklearn.preprocessing.minmax_scale, should easily solve your problem. 
e.g.:
from sklearn.preprocessing import minmax_scale
column_1 = foo[:,0] #first column you don't want to scale
column_2 = minmax_scale(foo[:,1], feature_range=(0,1)) #second column you want to scale
foo_norm = np.stack((column_1, column_2), axis=1) #stack both columns to get a 2d array
Should yield
array([[0.        , 0.        ],
       [0.13216   , 0.06609523],
       [0.25379   , 1.        ],
       [0.30874   , 0.09727968]])
Maybe you want to min-max scale between 0 and 1 both columns. In this case, use:
foo_norm = minmax_scale(foo, feature_range=(0,1), axis=0)
Which yields
array([[0.        , 0.        ],
       [0.42806245, 0.06609523],
       [0.82201853, 1.        ],
       [1.        , 0.09727968]])
note: Not to be confused with the operation that scales the norm (length) of a vector to a certain value (usually 1), which is also commonly referred to as normalization.
sklearn.preprocessing.MinMaxScaler can also be used (feature_range=(0, 1) is default):
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
v = foo[:,1]
v_scaled = min_max_scaler.fit_transform(v)
foo[:,1] = v_scaled
print(foo)
Output:
[[ 0.          0.        ]
 [ 0.13216     0.06609523]
 [ 0.25379     1.        ]
 [ 0.30874     0.09727968]]
Advantage is that scaling to any range can be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With