如何标准化加权列表?
How to normalize weighted list?
我想对元素列表和列表中的 select 随机元素进行加权。我有以下 javascript,它们是这样做的:
var generateWeighedList = function(list, weight) {
var weighed_list = [];
// Loop over weights
for (var i = 0; i < weight.length; i++) {
var multiples = weight[i] * 100;
// Loop over the list of items
for (var j = 0; j < multiples; j++) {
weighed_list.push(list[i]);
}
}
return weighed_list;
};
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [0.5, 0.2, 0.2, 0.1];
var weighed_list = generateWeighedList(list, weight);
console.log(weighed_list.length);
用户可以将权重指定为 1 到 100 之间的数字。输入将如下所示:
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [10, 50, 70, 90];
如何自定义Algorithmus,使权重列表最后的总和为100,并且每个元素的比例都为100%?
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [10, 50, 70, 90];
// build new array with length of weight
var weighed_list = Array.apply(null, { length: weight.length });
// get the sum of all weights
var sum = weight.reduce(function (a, b) { return a + b; }, 0);
// final take the ratio of every weight
weight.forEach(function (a, i) { weighed_list[i] = 100 * a / sum; });
我想对元素列表和列表中的 select 随机元素进行加权。我有以下 javascript,它们是这样做的:
var generateWeighedList = function(list, weight) {
var weighed_list = [];
// Loop over weights
for (var i = 0; i < weight.length; i++) {
var multiples = weight[i] * 100;
// Loop over the list of items
for (var j = 0; j < multiples; j++) {
weighed_list.push(list[i]);
}
}
return weighed_list;
};
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [0.5, 0.2, 0.2, 0.1];
var weighed_list = generateWeighedList(list, weight);
console.log(weighed_list.length);
用户可以将权重指定为 1 到 100 之间的数字。输入将如下所示:
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [10, 50, 70, 90];
如何自定义Algorithmus,使权重列表最后的总和为100,并且每个元素的比例都为100%?
var list = ['javascript', 'php', 'ruby', 'python'];
var weight = [10, 50, 70, 90];
// build new array with length of weight
var weighed_list = Array.apply(null, { length: weight.length });
// get the sum of all weights
var sum = weight.reduce(function (a, b) { return a + b; }, 0);
// final take the ratio of every weight
weight.forEach(function (a, i) { weighed_list[i] = 100 * a / sum; });