用于计算5星评级的算法

时间:2012-04-17 18:06:26

标签: javascript rating-system

我需要计算亚马逊网站上的五星评级。我已经做了足够的搜索来找到最好的算法,但我无法得到正确的答案。例如,如果这些是评级

5 star - 252
4 star - 124
3 star - 40
2 star - 29
1 star - 33

共478条评论

亚马逊已将此计算为“4.1星满分5星”。谁能告诉我这个数字是如何得出的?我只能做平均值才能得到这个。

14 个答案:

答案 0 :(得分:123)

这是一个加权平均值,您可以根据得票数来衡量每个评级:

(5*252 + 4*124 + 3*40 + 2*29 + 1*33) / (252+124+40+29+33) = 4.11 and change

答案 1 :(得分:10)

在evanmiller.org上有一篇关于这个主题的文章。他介绍并讨论了一些方法的优缺点,并提出了一种数学上可靠的加权和计算投票方式。

http://evanmiller.org/ranking-items-with-star-ratings.html

答案 2 :(得分:10)

如果您从头开始计算总体评分,那么此公式将对您有所帮助。

((Overall Rating * Total Rating) + new Rating) / (Total Rating + 1)

实施例

假设你到目前为止没有评级那么公式就像, 到目前为止整体评价为“0”。 总评分“0” 并给出评级为“4”

((0*0)+4)/1 = 4

如果总体评级是“4.11”总评分是“478”并且新评分  由一个用户给出的是“2”

然后公式就像

((4.11 * 478)+ 2)/479 // 479 is increment of new rating from 478

答案 3 :(得分:8)

是的,你可以将它们平均化:

(5 * 252 + 4 * 124 + 3 * 40 + 2 * 29 + 1 * 33)/ 478 = 4.11

答案 4 :(得分:4)

此评分系统基于weighted average加权平均值。也就是说,他们使用星星的权重来计算舍入到4.1的十进制值。例如:

Sum of (weight * number of reviews at that weight) / total number of reviews
(5*252 + 4*124 + 3*40 + 2*29 + 1*33) / 478 = 4.1

答案 5 :(得分:4)

Blindy的超级有用的回复,这里是基于它的PHP代码。有些人可能觉得有用。根据OP的例子,结果将是4.11:

$ratings = array(
5 => 252,
4 => 124,
3 => 40,
2 => 29,
1 => 33
);

function calcAverageRating($ratings) {

$totalWeight = 0;
$totalReviews = 0;

foreach ($ratings as $weight => $numberofReviews) {
    $WeightMultipliedByNumber = $weight * $numberofReviews;
    $totalWeight += $WeightMultipliedByNumber;
    $totalReviews += $numberofReviews;
}

//divide the total weight by total number of reviews
$averageRating = $totalWeight / $totalReviews;

return $averageRating;
}

答案 6 :(得分:2)

你可能想要检查这个算法:Calculating Average Rating the Right Way using PHP and MySQL - 不需要保存每个“星”(也就是评级等级)及其相应的投票数,然后每个必须从数据库中检索数千行你需要计算他们的平均时间。 (除非您想准确显示有多少人将给定项目评为1,2,3,4和5星)

答案 7 :(得分:1)

加权平均值,将星数乘以其权重,然后除以总评论数。

答案 8 :(得分:1)

根据您的问题,您的解决方案将是这样。

(Rate * TotalRatingOfThatRate)/ TotalNumberOfReviews之和

(((5 * 252)+(4 * 124)+(3 * 40)+(2 * 29)+(1 * 33))/(252 + 124 + 40 + 29 + 33)

输出将为4.1

答案 9 :(得分:0)

一种更好的方法,

rating = (sum_of_rating * 5)/sum_of_max_rating_of_user_count  

示例:

total users rated: 6  
sum_of_max_rating_of_user_count: 6 x 5 = 30  
sum_of_rating: 25

rating = (25 * 5) / 30

完成!

答案 10 :(得分:0)

使用Javascript

sudo chown -R hadoop:hadoop hadoop-3.2.0/

$ hadoop version

Hadoop 3.2.0
Source code repository https://github.com/apache/hadoop.git -r e97acb3bd8f3befd27418996fa5d4b50bf2e17bf

Compiled by sunilg on 2019-01-08T06:08Z

Compiled with protoc 2.5.0

From source with checksum d3f0795ed0d9dc378e2c785d3668f39
This command was run using /opt/hadoop-3.2.0/share/hadoop/common/hadoop-common-3.2.0.jar

答案 11 :(得分:0)

(星级总数/评论总数* 5)* 5

=答案

将js中的小数固定为1。

answer.toFixed(1);

例如5人的总评价为20星。

(20/5 * 5)* 5 = 4.0

答案 12 :(得分:0)

此外,我只是想为所有人编写实用而完整的代码。

我的Json对象数组

var yourRatingData =[{
        review_id:1,
        customer_id:5,
        customer_name:"Faysal",
        rating:5,
        review_content:"I like this product it's cool and best in quality"
    },
    {
        review_id:2,
        customer_id:6,
        customer_name:"Adams",
        rating:4,
        review_content:"It's quality product though price a bit high"
    },
    {
        review_id:3,
        customer_id:8,
        customer_name:"Jane",
        rating:3,
        review_content:"I like but should improve quality"
    },
    {
        review_id:4,
        customer_id:9,
        customer_name:"Julia",
        rating:1,
        review_content:"It's not good"
    }];

评分计算

let _5star = yourRatingData.filter(r=>r.rating==5).length;
let _4star = yourRatingData.filter(r=>r.rating==4).length;
let _3star = yourRatingData.filter(r=>r.rating==3).length;
let _2star = yourRatingData.filter(r=>r.rating==2).length;
let _1star = yourRatingData.filter(r=>r.rating==1).length;

//Sum of individual star.
let sumOfRating = parseInt( _5star + _4star + _3star + _2star + _1star );

//Total number of rating
let overallRating = parseInt( 5*_5star + 4*_4star + 3*_3star + 2*_2star +1*_1star );

//Average of all rating
let averageRating = parseFloat(overallRating/sumOfRating);

//Percentage of each star rating
let _5starPercentage = parseInt((_5star/totalRating)*100);
let _4starPercentage = parseInt((_4star/totalRating)*100);
let _3starPercentage = parseInt((_3star/totalRating)*100);
let _2starPercentage = parseInt((_2star/totalRating)*100);
let _1starPercentage = parseInt((_1star/totalRating)*100);

我认为这很有帮助。

答案 13 :(得分:-1)

使用C#

double rating = (double)(5 * star5 + 4 * star4 + 3 * star3 + 2 * star2 + 1 * star1) / (star1 + star2 + star3 + star4 + star5);
rating = Math.Round(rating, 1);