Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

python - Min-max normalisation of a NumPy array

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]]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...