本人 python 初学者,现在在算法课程中遇到一个问题,希望大家给点思路。
这是一个简单电影推荐算法的题目。题目会给 3 个 list. movies = ["Parasite","1917","Ford v Ferrari","Jojo Rabbit","Joker"]
similarities = [["Parasite", "1917"],
["Parasite", "Jojo Rabbit"],
["Joker", "Ford v Ferrari"]]
friends = [["Joker"],
["Joker","1917"],
["Joker"],
["Parasite"],
["1917"],
["Jojo Rabbit", "Joker"]]
Movies 里有 5 部电影。Similarities 代表里面的两部电影是相关的,所以 parasite 和 1917 相关,同样 1917 和 jojo rabbit 也相关,因为它们都和 parasite 相关。Friends 里代码有哪些电影被看过。
我们要得出的结果是推荐 观看度和独特性最高的那部电影。 观看度就是 friends 里出现最多的那部电影。那就是 joker
• Joker - 4
• 1917 - 2
• Parasite - 1
• Jojo Rabbit - 1
• Ford v Ferrari - 0
独特性计算 Uniqueness is 1 divided by the mean number of similar movies that the user's friends have already seen. Similar movies have transitive property: if movie 1 is similar to movie 2 and movie 2 is similar to movie 3 -> movie 1 is similar to movie 3 and vice versa.
下面是每部电影的相似电影
• Joker - 1 (Ford v Ferrari)
• 1917 - 2 (Parasite, Jojo Rabbit)
• Parasite - 2 (1917, Jojo Rabbit)
• Jojo Rabbit - 2 (Parasite, 1917)
• Ford v Ferrari - 1 (Joker)
下面是朋友里看过这些相似电影的统计
• Joker - 0 (None saw Ford v Ferrari)
• 1917 - 1 (Parasite), 1 (Jojo Rabbit)
• Parasite - 2 (1917) , 1 (Jojo Rabbit)
• Jojo Rabbit - 1 (Parasite), 2 (1917)
• Ford v Ferrari - 4 (Joker)
然后计算平均数:
• Joker - mean(0) = 0
• 1917 - mean(1,1) = 1
• Parasite - mean(2 ,1) = 1.5
• Jojo Rabbit - mean(1,2) = 1.5
• Ford v Ferrari - mean(4) = 4
In the end we need to return movie with highest score which is calculated like this: F/S, where F = number of friends who have seen this movie and S = mean of the number of similar movies seen for each friend Let's find out what movie should we recommend:
• Joker - 4/0 = 0
• 1917 - 2/1 = 2
• Parasite - 1/1.5 = 0.66666
• Jojo Rabbit - 1/1.5 = 0.66666
• Ford v Ferrari - 0/4 = 0
这不是一个超级复杂的算法推荐题(但是我仍然搞不定),班里的一个同学说只需要用 dictionaries with sets (hashable data structures) and the logics loop 就能解决了,不需要用 DFS 算法之类的。
希望大家能给点思路,最好就只用基本数据结构。
Thanks