用于计算5星级的算法


79

我需要像亚马逊网站上那样计算5星级。我已经进行了足够的搜索以找到最佳的算法,但是我找不到正确的答案。例如,如果这些是评分

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

共478条点评

亚马逊计算得出这是“ 5星中的4.1星”。谁能告诉我这个数字是怎么得出的?我不能仅仅通过平均来得到这个。


3
embed.plnkr.co/5STGsgya9mq7HLrbJtrT一个Verbose程序,用于显示平均星计算。
Sanju,2017年

Answers:


168

这是一个加权平均值,您可以在其中将每个评分与其获得的票数进行权衡:

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

11
这与“常规”平均值有何不同?
杰夫

@Jeff,权重不同于一个,这就是大多数人所说的“平均值”。
布林迪2012年

2
这与常规平均值相同
Daniel

1
是的,它与常规平均值相同。我们(添加总星数)除以总用户数。它看起来像分子的乘法加权coz。但是,如果改写它,我们将本质上将所有5、4等以此类推。由于5是252次,所以最终为5 * 252,依此类推。
程序员

20

如果您从头开始计算总体评分,那么此公式将为您提供帮助。

((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


8

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;
}

如何构建上面的$ ratings数组

伪代码示例,但假设您有一个名为“ ratings”的表和一个名为“ rating”的列,则该伪代码应能解释在数据库中存储信息时如何构建$ ratings数组。在这种情况下,这是1次加入,您需要进行4次加入才能获得所有评分,但这应该可以帮助您入门:

SELECT count(c1.rating) as one_star, count(c2.rating) as two_star  
FROM ratings c1 
LEFT OUTER JOIN
ratings c2
ON
c1.id = c2.id
WHERE
c1.rating = 1
AND
c2.rating = 2

评论中建议的另一种方法

SELECT SUM(rating = 1) AS one_s ,SUM(rating = 2) AS two_s ,SUM(rating = 3) as three_s FROM reviews where product_id = 9

您已经制作了评分数组,如何使用mysql制作此数组。我的数据保存在数据库中
Ali Raza

1
@AliRaza我已经更新了答案,您需要执行多个连接或使用5个单独的查询提取数据
Robert Sinclair

1
我会说你是真正给我这个主意的人,而我只是在努力,这个查询对我SELECT SUM(rating = 1) AS one_s ,SUM(rating = 2) AS two_s ,SUM(rating = 3) as three_s FROM reviews where product_id = 9
有用

8

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

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

6

一种更好的方法

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

做完了!


什么是5中使用的sum_of_max_rating_of_user_count立场?是任何用户给的最高评价吗?
andho

请说明...... sum_of_max_rating_of_user_count:6 x 5 = 30 ???
thevikasnayak

6是已评级的用户总数。5是最大星级(例如,通常我们使用5星级)
Asad

4

该评估系统基于加权平均值加权平均值。也就是说,他们使用以星为单位的权重来计算一个四舍五入到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

2

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


2

您可能需要检查一下该算法:使用PHP和MySQL以正确的方式计算平均评分-无需保存每个“星级”(也称为评分等级)及其对应的投票数,然后必须从表单中检索数千行每次需要计算数据库平均值时。(除非您想确切显示有多少人将1、2、3、4和5星评为给定项目)


1

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

(Rate * TotalRatingOfThatRate)/ TotalNumberOfReviews之和

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

输出将是4.1



0

用Java语言编写

function calcAverageRating(ratings) {

  let totalWeight = 0;
  let totalReviews = 0;

  ratings.forEach((rating) => {

    const weightMultipliedByNumber = rating.weight * rating.count;
    totalWeight += weightMultipliedByNumber;
    totalReviews += rating.count;
  });

  const averageRating = totalWeight / totalReviews;

  return averageRating.toFixed(2);
}


const ratings = [
  {
    weight: 5,
    count: 252
  },
  {
    weight: 4,
    count: 124
  },
  {
    weight: 3,
    count: 40
  },
  {
    weight: 2,
    count: 29
  },
  {
    weight: 1,
    count: 33
  }
];

console.log(calcAverageRating(ratings));

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);

我认为这很有帮助。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.