How can I sort books by review, but not by average review?

别来无恙 提交于 2021-02-11 13:56:47

问题


First of all, I have no background and I am new to this kind of science.

Here is my problem. I have a list of books with reviews by readers (let's say that the user can give 1 to 5 stars to the books).

Now I would like to sort the books from the best to the worst, according to the reviews, but doing the average of the reviews seems wrong, because a book with a single review of 5 stars would be considered better than a book with many reviews of 4 and 5 stars.

What are my options here and is there any python specific librairies to do that ?

Thank you !


回答1:


Here's a simple example of using "pseudo-reviews" (as described in https://stats.stackexchange.com/questions/173683/sorting-products-by-reviews-considering-the-number-of-reviews) to sort a list:

from statistics import mean
from typing import List, NamedTuple


class Book(NamedTuple):
    title: str
    reviews: List[int]
    # other useful info might go here?


books = [
    Book("The Big Orange Splot", [5, 5]),
    Book("Lizard Music", [5, 5, 5, 5, 4, 5]),
    Book("Young Adult Novel", [2, 2, 5, 5, 5, 5, 5, 5]),
    Book("Fishwhistle", [4, 4, 5, 4, 5]),
    Book("The Davinci Code", [2, 2, 5, 2, 3, 4, 2, 1])
]

books.sort(
    key=lambda book: mean(book.reviews + [3, 3, 3]),
    reverse=True
)

for book in books:
    print(
        f"{book.title}: "
        f"{mean(book.reviews):.1f} stars "
        f"({len(book.reviews)} reviews)"
    )
Lizard Music: 4.8 stars (6 reviews)
Young Adult Novel: 4.2 stars (8 reviews)
Fishwhistle: 4.4 stars (5 reviews)
The Big Orange Splot: 5.0 stars (2 reviews)
The Davinci Code: 2.6 stars (8 reviews)

Note that the sort() call adds 3 fake 3-star reviews before calling mean() for the purpose of skewing books with small numbers of reviews toward 3, but later on when we display the reviews we use the "real" mean rating without the fake entries.




回答2:


Rather than average customer review, you could also have a more abstract option of "most popular"? Amazon gets round the issue you describe by having an "Amazon's Choice" for what one must assume to be the product with the best average reviews AND having sold many units

One way to look at it is to think about how many "points" something has, and how many "points" it has had the opportunity to score. If an item has been reviewed ten times, and the max score was 5, then it has had the opportunity to score 50 points. You could set a threshold in this way, to say that only items that have had to opportunity to score e.g. 100 points are included in the sorting method for "most popular". This can be an abstract concept to the end-user, after all we have no idea exactly why something is "Amazon's Pick" but it seems to make sense when you use their website.

In terms of coding this, the simplest way would be to only include books in your "most popular" sorting method that have at least x reviews



来源:https://stackoverflow.com/questions/61461316/how-can-i-sort-books-by-review-but-not-by-average-review

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!