将仅包含正数据的列表归一化为包含负和正数值的范围内。

huangapple go评论68阅读模式
英文:

Normalize a list containing only positive data into a range comprising negative and positive values

问题

我有一个只包含正值的数据列表,例如下面的列表:

data_list = [3.34, 2.16, 8.64, 4.41, 5.0]

是否有一种方法可以将这个列表归一化到跨越从-1到+1的范围内?

2.16应对应于标准化范围内的-1,而值8.64应对应于+1

我找到了一些关于如何将同时包含负值和正值的列表进行归一化的主题,但如何将仅包含正值或仅包含负值的列表归一化为一个新的标准化列表,跨越从负数到正数的范围呢?

英文:

I have a list of data that only contains positive values, such as the list below:

data_list = [3.34, 2.16, 8.64, 4.41, 5.0]

Is there a way to normalize this list into a range that spans from -1 to +1?

The value 2.16 should correspond to -1 in the normalized range, and the value 8.64 should correspond to +1.

I found several topics treating the question of how one can normalize a list that contains negative and positive values. But how can one normalize a list of only positive or only negative values into a new normalized list that spans from the negative into the positive range?

答案1

得分: 1

使用[tag:numpy],您可以使用interp

#pip install numpy
import numpy as np

data_list = [0.54, 2.16, 8.35, 0.35, 1.1]

out = np.interp(x=data_list, xp=(min(data_list), max(data_list)), fp=(-1, +1)).tolist()

输出:

print(out)

# [-0.9525, -0.5475, 1.0, -1.0, -0.8125]
英文:

With [tag:numpy], you can use interp:

#pip install numpy
import numpy as np

data_list = [0.54, 2.16, 8.35, 0.35, 1.1]

out = np.interp(x=data_list, xp=(min(data_list), max(data_list)), fp=(-1, +1)).tolist()

Output :

print(out)

# [-0.9525, -0.5475, 1.0, -1.0, -0.8125]

答案2

得分: 1

不需要为这样的任务导入库。归一化数学运算非常简单。

要在0到1之间进行归一化,取每个值,减去列表的最小值,然后除以最大值减去最小值。

def normalise(data):
    data_max = max(data)
    data_min = min(data)
    for i, n in enumerate(data):
        data[i] = (n - data_min) / (data_max - data_min)
    return data

要改变缩放范围,您需要将其乘以范围的跨度(即 abs(max_value-min_value)),然后加上下限。

def normalise_to_range(data, min_value, max_value):
    data_max = max(data)
    data_min = min(data)
    span = abs(max_value - min_value)
    for i, n in enumerate(data):
        data[i] = span * (n - data_min) / (data_max - data_min) + min_value
    return data

使用您的数据并将范围设置为-1到1:

data_list = [0.54, 2.16, 8.35, 0.35, 1.1]
normalise_to_range(data_list, -1, 1)

#[-0.9525, -0.5475, 1.0, -1.0, -0.8125]
英文:

You don't need to import libraries for a task like this. The normalisation maths is quite simple.

To normalise between 0 and 1, take each value, subtract the minimum value of the list, then divide by the maximum value minus the minimum value.

def normalise(data):
	data_max=max(data)
	data_min=min(data
	for i,n in enumerate(data):
		data[i]=(n-data_min)/(data_max-data_min)
	return(data)

To then change the range that it is scaled to, you need to multiply it by the span of the range (i.e., abs(max_value-min_value)) then add the lower limit.

def normalise_to_range(data,min_value,max_value):
	data_max=max(data)
	data_min=min(data)
	span=abs(max_value-min_value)
	for i,n in enumerate(data):
		data[i]=span*(n-data_min)/(data_max-data_min)+min_value
	return(data)

Using your data and setting the range to -1,1:

data_list = [0.54, 2.16, 8.35, 0.35, 1.1]
normalise_to_range(data_list,-1,1)

#[-0.9525, -0.5475, 1.0, -1.0, -0.8125]

答案3

得分: 1

以下是翻译好的内容:

虽然有现成的库函数可用,但您可以很容易地使用简单的列表推导来复制该逻辑。

data_list = [3.34, 2.16, 8.64, 4.41, 5.0]

minL = min(data_list)
maxL = max(data_list)

normalised = [((x - minL) * 2)/(maxL - minL) - 1 for x in data_list]

print(normalised) # [-0.6358024691358026, -1.0, 1.0, -0.3055555555555556, -0.1234567901234569]
英文:

While there are ready-made lib functions available, you can very easily replicate that logic with a simple list comprehension.

data_list = [3.34, 2.16, 8.64, 4.41, 5.0]

minL = min(data_list)
maxL = max(data_list)

normalised = [((x - minL) * 2)/(maxL - minL) - 1 for x in data_list]

print(normalised) # [-0.6358024691358026, -1.0, 1.0, -0.3055555555555556, -0.1234567901234569]

huangapple
  • 本文由 发表于 2023年4月13日 15:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定