Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

Consider the below 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 might I standardize/normalize the Y segment of this array? So it gives me something like: 

[[  0.       0.   ]

 [  0.13216  0.06 ]

 [  0.25379  1    ]

 [  0.30874  0.097]]

1 Answer

0 votes
by (26.4k points)

It would seem that you can perform min-max normalization on the last segment/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]])

You can also try using sklearn.preprocessing.normalize, but it will gives you a but different result.

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]])

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 17, 2019 in Python by Sammy (47.6k points)
0 votes
2 answers

Browse Categories

...