将小数字转换为从0到10的刻度

2024-04-26 15:02:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个URL列表->;小数目。数字代表每个URL的重要性

url, value
https://mywebsite.com/p/1, 0.00212
https://mywebsite.com/p/2, 0.00208
https://mywebsite.com/p/3, 0.00201
https://mywebsite.com/p/4, 0.00138
https://mywebsite.com/p/5, 0.00067
https://mywebsite.com/p/1, 0.00001
...

假设值之和=1 我想在0-10的范围内表示这些数字,并保持数字之间的比率差

url, value, scaled_value
https://mywebsite.com/p/1, 0.00212, 10
https://mywebsite.com/p/2, 0.00208, 9
https://mywebsite.com/p/3, 0.00201, 9
https://mywebsite.com/p/4, 0.00138, 6
https://mywebsite.com/p/5, 0.00067, 3
https://mywebsite.com/p/1, 0.00001, 1
...

类似这样的情况(我不知道这里的比率差是否保持在tho) 有人能帮我学数学吗? 谢谢

#更新

多亏@annZen的帮助,我尝试了两种方法,但结果不同,我不知道为什么。如果有人能帮忙

enter image description here

以下是我使用的两个公式:

res1 = round(x*9/maxpri)+1
res2 = round(((x-minpri)/(maxpri-minpri))*10, 2)

Tags: httpsgtcomurl列表value代表数字
3条回答

如果您想保持两个数字之间一定的比率差,可以将最小的数字设置为1,然后每隔一个数字设置为num/smallest

这种方法的问题在于它不能保证每个URL都设置为0 - 10中的一个数字。在上面的示例中,它将分别将数字设置为212, 208, 201, 138, 67, and 1

如果确实需要将每个数字设置为介于某个指定范围之间,则首先将最小的URL设置为重要性0,将最大的URL设置为重要性10。然后,所有其他点将位于斜率为(max value - min value)/10的线上。下图展示了这一概念:

enter image description here

在这张图片中,点的y值表示它们的URL值,x坐标表示点的重要性

如果这是针对生产代码的,那么我建议使用csv.DictReadercsv.DictWriter,以便在稍后返回时易于阅读。例如:

from csv import DictReader, DictWriter

scaled_field_name = 'scaled_value'

with open('input.csv') as fin:
    csvin = DictReader(fin, skipinitialspace=True)
    rows = list(csvin)

values = [float(row['value']) for row in rows]
min_value = min(values)
max_value = max(values)
for row, value in zip(rows, values):
    scaled = 10 * (value - min_value) / (max_value - min_value)
    row[scaled_field_name] = str(round(scaled))

with open('output.csv', 'w') as fout:
    csvout = DictWriter(fout, csvin.fieldnames + [scaled_field_name])
    csvout.writerows(rows)

(注意:它不会在逗号后写空格,但这对于CSV应该是正常的。)

这里有一个方法:

with open('file.txt', 'r') as p:
    lst = p.read().splitlines() # List all the lines of the file

lst2 = [float(i.split(', ')[1]) for i in lst[1:]] # List all the floats

num = [round(a*9/max(lst2))+1 for a in lst2] # List all the scaled numbers

for i,(l,n) in enumerate(zip(lst,['scaled_value']+num)):
    lst[i] = f"{l}, {n}" # Add the 'scaled_value' column

with open('file.txt', 'w') as p:
    p.write('\n'.join(lst)) # Write the updated data into the file

之前:

url, value
https://mywebsite.com/p/1, 0.00212
https://mywebsite.com/p/2, 0.00208
https://mywebsite.com/p/3, 0.00201
https://mywebsite.com/p/4, 0.00138
https://mywebsite.com/p/5, 0.00067
https://mywebsite.com/p/1, 0.00001

之后:

url, value, scaled_value
https://mywebsite.com/p/1, 0.00212, 10
https://mywebsite.com/p/2, 0.00208, 10
https://mywebsite.com/p/3, 0.00201, 10
https://mywebsite.com/p/4, 0.00138, 7
https://mywebsite.com/p/5, 0.00067, 4
https://mywebsite.com/p/1, 0.00001, 1



更新:

我的代码中进行转换的部分是:

num = [round(a*9/max(lst2))+1 for a in lst2]

其中lst2只是从文件中提取的浮点列表。你更新了问题,让我解释

res1 = round(x*9/maxpri)+1
res2 = round(((x-minpri)/(maxpri-minpri))*10, 2)

让我们先看看我的理解列表:

num1 = [round(x*9/max(lst2))+1 for x in lst2]
num2 = [round(((x-min(lst2))/(max(lst2)-min(lst2)))*10, 2) for x in lst2]
print(num1)
print(num2)

输出:

[10, 10, 10, 7, 4, 1]
[10.0, 9.81, 9.48, 6.49, 3.13, 0.0]

第一个最明显的区别是我把答案四舍五入到最接近的整数。如果没有它,它将是:

num1 = [round(x*9/max(lst2), 2)+1 for x in lst2]
num2 = [round(((x-min(lst2))/(max(lst2)-min(lst2)))*10, 2) for x in lst2]
print(num1)
print(num2)

输出:

[10.0, 9.83, 9.53, 6.86, 3.84, 1.04]
[10.0, 9.81, 9.48, 6.49, 3.13, 0.0]

这些值现在非常接近,但还有一件事。我的代码假设缩放值的最小值为1,因为我在您的帖子中看到https://mywebsite.com/p/1, 0.00001, 1。我现在意识到你说的是0-10,不是1-10。因此,另一种方法是将9(10-1=9)更改为10(10-0=10),并删除+1

round(x*9/max(lst2), 2)+1
round(x*10/max(lst2), 2)

num1 = [round(x*10/max(lst2), 2) for x in lst2]
num2 = [round(((x-min(lst2))/(max(lst2)-min(lst2)))*10, 2) for x in lst2]
print(num1)
print(num2)

输出:

[10.0, 9.81, 9.48, 6.51, 3.16, 0.05]
[10.0, 9.81, 9.48, 6.49, 3.13, 0.0]

还有一点不同,那是因为我假设列中的最小值是0,因为您没有显示整个数组。但在这种情况下,它是0.00001。因此,请继续:

num = [round(((x-min(lst2))/(max(lst2)-min(lst2)))*10, 2) for x in lst2]

小结:我的代码假设您希望将数字从1缩放到10,而不是从0缩放到10,并且我的代码假设您的数据的最小值为0,情况可能并非如此。

相关问题 更多 >