[英]Min-max scaling along rows in numpy array
I have a numpy array and I want to rescale values along each row to values between 0 and 1 using the following procedure: 我有一个numpy数组,我想使用以下过程将每一行的值重新缩放为0到1之间的值:
If the maximum value along a given row is X_max
and the minimum value along that row is X_min
, then the rescaled value ( X_rescaled
) of a given entry ( X
) in that row should become: 如果给定行上的最大值为
X_max
而该行上的最小值为X_min
,则该行中给定条目( X
)的重缩放值( X_rescaled
)应该变为:
X_rescaled = (X - X_min)/(X_max - X_min)
As an example, let's consider the following array ( arr
): 例如,让我们考虑以下数组(
arr
):
arr = np.array([[1.0,2.0,3.0],[0.1, 5.1, 100.1],[0.01, 20.1, 1000.1]])
print arr
array([[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00],
[ 1.00000000e-01, 5.10000000e+00, 1.00100000e+02],
[ 1.00000000e-02, 2.01000000e+01, 1.00010000e+03]])
Presently, I am trying to use MinMaxscaler
from scikit-learn
in the following way: 目前,我正在尝试通过以下方式使用
scikit-learn
MinMaxscaler
:
from sklearn.preprocessing import MinMaxScaler
result = MinMaxScaler(arr)
But, I keep getting my initial array, ie result
turns out to be the same as arr
in the aforementioned method. 但是,我一直在获取初始数组,即
result
与上述方法中的arr
相同。 What am I doing wrong? 我究竟做错了什么?
How can I scale the array arr
in the manner that I require (min-max scaling along each axis?) Thanks in advance. 如何以所需的方式缩放数组
arr
(沿每个轴的最小-最大缩放?)预先感谢。
MinMaxScaler
is a bit clunky to use; MinMaxScaler
使用起来有点笨拙。 sklearn.preprocessing.minmax_scale
is more convenient. sklearn.preprocessing.minmax_scale
更加方便。 This operates along columns, so use the transpose: 这沿列操作,因此请使用转置:
>>> import numpy as np
>>> from sklearn import preprocessing
>>>
>>> a = np.random.random((3,5))
>>> a
array([[0.80161048, 0.99572497, 0.45944366, 0.17338664, 0.07627295],
[0.54467986, 0.8059851 , 0.72999058, 0.08819178, 0.31421126],
[0.51774372, 0.6958269 , 0.62931078, 0.58075685, 0.57161181]])
>>> preprocessing.minmax_scale(a.T).T
array([[0.78888024, 1. , 0.41673812, 0.10562126, 0. ],
[0.63596033, 1. , 0.89412757, 0. , 0.314881 ],
[0. , 1. , 0.62648851, 0.35384099, 0.30248836]])
>>>
>>> b = np.array([(4, 1, 5, 3), (0, 1.5, 1, 3)])
>>> preprocessing.minmax_scale(b.T).T
array([[0.75 , 0. , 1. , 0.5 ],
[0. , 0.5 , 0.33333333, 1. ]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.