[英]How to normalize an array between min and max value
I have an array:我有一个数组:
list = [[2310.01, 2640.14, 2710.63, 2926, 2700.12],
[2014.45, 2160, 2430.65, 2700.65, 2714.63]]
I need to normalize each row in a 2D list between (min=-.1, max=.1)
.我需要在(min=-.1, max=.1)
之间规范化二维列表中的每一行。 All methods can normalize the data between [0,1]
or [-1,1]
.所有方法都可以标准化[0,1]
或[-1,1]
之间的数据。 But, since I have 2D array, I need to normalize each row between some min/max value, for example: (-.1, .1)
.但是,由于我有 2D 数组,因此我需要对某个最小值/最大值之间的每一行进行归一化,例如: (-.1, .1)
。
I am using Python and MATLAB, hope I can get answers with python or matlab.我正在使用 Python 和 MATLAB,希望我可以用 python 或 matlab 得到答案。
In MATLAB, you can do it as following:在 MATLAB 中,您可以执行以下操作:
Normalize to range [-1, 1]:归一化到范围 [-1, 1]:
norm_list = ((list - min(list, [], 2)) ./ (max(list, [], 2) - min(list, [], 2)))*2 - 1
Not the best, but quickest solution I thought of...不是最好的,但我想到的最快的解决方案......
Update - normalizing to general destination range:更新- 标准化为一般目标范围:
lo_out = -0.1
hi_out = 0.1
range_out = hi_out - lo_out
%Normalize to range [0, 1]:
%norm_list = (list - min(list, [], 2)) ./ (max(list, [], 2) - min(list, [], 2))
%Normalize to range [lo_out, hi_out]:
lo_in = min(list, [], 2); %Minimum of each row
hi_in = max(list, [], 2); %Maximum of each row
range_in = hi_in - lo_in; %Range of each row
norm_list = ((list - lo_in) ./ range_in) * range_out + lo_out
The simplest way will be to do min-max normalization最简单的方法是进行最小-最大归一化
np.array(list)
array = list[:] - np.min(list) / (np.max(list) - np.min(list))
array = 2*array - 1
Now the array is normalised between -1 and 1现在数组在 -1 和 1 之间标准化
In R2018a and later, you can use the normalize function directly in MATLAB to do this.在 R2018a 及更高版本中,您可以直接在 MATLAB 中使用 normalize 函数来执行此操作。
A = magic(3)
A =
8 1 6
3 5 7
4 9 2
normalize(A,2,'range',[-1 1])
ans =
1.0000 -1.0000 0.4286
-1.0000 0 1.0000
-0.4286 1.0000 -1.0000
There is a great function in Matlab called "mapminmax" developed for this job. Matlab 中有一个很棒的函数叫做“mapminmax”,就是为这项工作开发的。
x=data;
[x_normalized, PS]=mapinmax(x',0,1); % It makes normalization in the range of 0-1.
x_normalized=x_normalized';
The function examines the rows, for these reason we have to do the x 'operation because we want to normalize according to the columns.该函数检查行,出于这些原因,我们必须执行 x ' 操作,因为我们想根据列进行归一化。 The PS parameter will represent the parameters required when we want to convert it to the actual value before normalization. PS参数将代表我们要在归一化之前将其转换为实际值时所需的参数。
x = (mapminmax('reverse',x_normalized',PS))' % Obtaining the true value of x.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.