94

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

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

共 478 条评论

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

4

13 回答 13

196

这是一个加权平均值,你可以用它获得的票数来衡量每个评级:

(5*252 + 4*124 + 3*40 + 2*29 + 1*33) / (252+124+40+29+33) = 4.11 and change
于 2012-04-17T18:09:30.150 回答
24

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

公式

((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
于 2015-09-17T13:33:43.280 回答
13

一个更好的方法来做到这一点,

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

完毕!

于 2018-11-08T08:25:26.633 回答
10

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

(5 * 252 + 4 * 124 + 3 * 40 + 2 * 29 + 1 * 33) / 478 = 4.11
于 2012-04-17T18:08:55.117 回答
9

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 数组,假设您有一个名为“ratings”的表和一个名为“rating”的列。在这种情况下,它是 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
于 2016-07-14T15:46:20.930 回答
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
于 2012-04-17T18:11:19.180 回答
2

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

于 2012-04-17T18:11:26.743 回答
1

在Javascript中

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));
于 2019-06-12T05:01:15.687 回答
1

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

Sum of (Rate*TotalRatingOfThatRate)/ TotalNumberOfReviews

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

输出将是 4.1

于 2020-03-13T10:05:06.577 回答
1

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

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

我认为这很有帮助。

于 2020-10-12T06:18:44.607 回答
0

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

= 回答

将 js 中的小数固定为 1。

answer.toFixed(1);

例如 5 人的总评论是 20 星。

(20/5*5)*5 = 4.0

于 2019-10-04T08:07:34.317 回答
0

这是使用颤振的示例方法,

double starRating = 5.0;
  getRating() {
    // ! Check Already Accepted by others or Not -----------------------------------
    //
    int totalof5s = 0;
    int totalof4s = 0;
    int totalof3s = 0;
    int totalof2s = 0;
    int totalof1s = 0;
    //
    FirebaseFirestore.instance
        .collection('usersRating')
        .where("passengerUid", isEqualTo: "GNblJJJsjicaA2vkXJNJ6XCAiwa2")
        .snapshots()
        .forEach((querySnapshot) {
      if (querySnapshot.size > 0) {
        querySnapshot.docs.forEach((element) async {
          if (element["rating"] == 5) {
            totalof5s++;
          } else if (element["rating"] == 4) {
            totalof4s++;
          } else if (element["rating"] == 3) {
            totalof3s++;
          } else if (element["rating"] == 2) {
            totalof2s++;
          } else if (element["rating"] == 1) {
            totalof1s++;
          }
          //
          if (this.mounted) {
            setState(() {
              starRating = (5 * totalof5s +
                      4 * totalof4s +
                      3 * totalof3s +
                      2 * totalof2s +
                      1 * totalof1s) /
                  (totalof5s + totalof4s + totalof3s + totalof2s + totalof1s);
            });
          }
        });
        
      } else {
        // This is default one in any case these user doesn't have any rating document exists
        if (this.mounted) {
          setState(() {
            starRating = 5.0;
          });
        }
      }
    });
  }
于 2021-08-24T10:30:38.467 回答
-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);
于 2020-06-07T11:32:40.367 回答