简体   繁体   English

如何计算加权平均值?

[英]How to calculate a weighted mean?

My language is PHP, but the algorithm should be fairly universal. 我的语言是PHP,但算法应该相当普遍。

I have an associative array of (let's say) ratings and number of times that rating has been given. 我有一个(比方说)评级的关联数组和给出评级的次数。

$ratings = array(
    1 => 1,
    2 => 3,
    3 => 6,
    4 => 3,
    5 => 3
);

This is the equivalent of: [1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5] , but given the numbers I'm working with, it would be quite inefficient to convert from the first form to the second. 这相当于: [1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5] ,但考虑到我正在使用的数字,从第一种形式转换到第二种形式是非常低效的。

What would be the algorithm to calculate the mean of the above numbers? 计算上述数字平均值的算法是什么?

Try this: 尝试这个:

$total = 0;
$count = 0;
foreach($ratings as $number=>$frequency) {
  $total += $number * $frequency;
  $count += $frequency;
}
return $total / $count;

Wouldn't this work? 这不行吗?

$total = 0;
$sum = 0;
foreach ($ratings as $k => $v) {
  $total += $k * $v;
  $sum += $v;
}
echo $total / $sum;

EDIT: Well, I look silly, since someone beat me to it. 编辑:嗯,我看起来很傻,因为有人打败了我。 Oh well. 那好吧。

Doubt I can beat the accepted answer, but I find that built in looping functions run faster than scripted loops. 怀疑我可以击败接受的答案,但我发现内置循环函数比脚本循环运行得更快。 Not sure how well the calls to $multiply are going to be optimized. 不确定对$ multiply的调用将如何优化。 If this is really slow then I expect someone will point it out in a comment. 如果这真的很慢,那么我希望有人会在评论中指出它。

function multiply( $k , $v ) { return $k * $v; }
return array_sum( array_map( 'multiply' , array_keys($ratings) , $ratings ) ) / array_sum( $ratings );

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

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