简体   繁体   English

如何确保任意数量的权重之和为1(Python)?

[英]How do a make sure an arbitrary number of weights sum to 1 (Python)?

I have a simulated annealing algorithm and I have a function like 我有一个模拟的退火算法,并且有一个类似的函数

result = w1*x1 + w2*x2 + ... + wn*xn

every loop of the simulated annealing when new w values are chosen how do you make sure that the sum of w always equal to 1 and that no individual w value is less that 0? 选择新的w值时,模拟退火的每个循环如何确保w的总和始终等于1,并且每个w值都不小于0?

Thank you very much folks! 非常感谢大家!

  1. Use NumPy. 使用NumPy。
  2. That way, instead of result = w1*x1 + w2*x2 + ... + wn*xn you can do result = np.dot(w, x) . 这样,您可以执行result = np.dot(w, x)而不是result = w1*x1 + w2*x2 + ... + wn*xn
  3. The condition you want on w seems like it could come from: 您想要的w条件似乎可能来自:

     non_negative_w = np.abs(w) sum_w = np.sum(non_negative_w) normalized_non_negative_w = non_negative_w / sum_w 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM