编写最小-最大缩放器功能 [英] Writing Min-Max scaler function
本文介绍了编写最小-最大缩放器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python中编写一个用于计算最小-最大比例的函数,以返回列表.
I want to write a function for calculating Min-Max scale in python that return a list.
x = [1, 2, 3, 4]
def normalize(x):
for i in range(len(x)):
return [(x[i] - min(x)) / (max(x) - min(x))]
然后调用该函数:
normalize(x):
结果:
[0.0]
我期望结果是:
[0.00, 0.33, 0.66, 1.00]
推荐答案
基于@shehan的解决方案:
Based on @shehan's solution:
x = [1, 2, 3, 4]
def normalize(x):
return [round((i - min(x)) / (max(x) - min(x)), 2) for i in x]
print(normalize(x))
完全给您您想要的.与其他解决方案不同,结果经过了四舍五入(这就是您想要的).
gives you exactly what you wanted. The result is rounded up unlike other solutions (as that's what you wanted).
结果:
[0.0, 0.33, 0.67, 1.0]
答案的循环版本,以便op可以理解:
For loop version of the answer so that op could understand:
x = [1, 2, 3, 4]
def normalize(x):
# A list to store all calculated values
a = []
for i in range(len(x)):
a.append([(x[i] - min(x)) / (max(x) - min(x))])
# Notice I didn't return here
# Return the list here, outside the loop
return a
print(normalize(x))
这篇关于编写最小-最大缩放器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文