我需要像亚马逊网站上那样计算5星级。我已经进行了足够的搜索以找到最佳的算法,但是我找不到正确的答案。例如,如果这些是评分
5 star - 252
4 star - 124
3 star - 40
2 star - 29
1 star - 33
共478条点评
亚马逊计算得出这是“ 5星中的4.1星”。谁能告诉我这个数字是怎么得出的?我不能仅仅通过平均来得到这个。
Answers:
在evanmiller.org上,有关该主题的文章非常多。他仔细研究了几种方法的利弊,并提出了一种数学上可靠的加权和计票方法。
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
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
一种更好的方法
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
立场?是任何用户给的最高评价吗?
您可能需要检查一下该算法:使用PHP和MySQL以正确的方式计算平均评分-无需保存每个“星级”(也称为评分等级)及其对应的投票数,然后必须从表单中检索数千行每次需要计算数据库平均值时。(除非您想确切显示有多少人将1、2、3、4和5星评为给定项目)
(星级总数/评论总数* 5)* 5
=答案
将js中的小数固定为1。
answer.toFixed(1);
范例5人的总评论是20星。
(20/5 * 5)* 5 = 4.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));
另外,我只是想为所有人编写实用而完整的代码。
我的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);
我认为这很有帮助。
与C#
double rating = (double)(5 * star5 + 4 * star4 + 3 * star3 + 2 * star2 + 1 * star1) / (star1 + star2 + star3 + star4 + star5);
rating = Math.Round(rating, 1);