Priporočilni sistem: navodila za implementacijo skupinskega filtriranja
Napovedovanje ocen s podobnostjo med produkti (6)
Napišite razred ItemBasedPredictor, ki v konstruktorju sprejme dva parametra: min_values in threshold (privzete vrednosti obeh naj bodo 0). Razred naj na podlagi podobnosti med produkti izračuna pričakovano oceno produktov za izbranega uporabnika. Podobnost izračunajte s popravljeno cosinusno razdaljo. V razredu implementirajte metode fit in predict ter similarity(self, p1, p2), ki vrne podobnost med produktoma p1 in p2. Če je izračunana podobnost med produktoma manjša od threshold ali če imamo manj kot min_values uporabnikov, ki je ocenilo oba filma, naj bo podobnost med produktoma 0. Predlagam, da v metodi fit izračunate vse podobnosti in jih v predict in fit le uporabljate.
Vzemimo le filme, ki imajo vsaj 1000 ocen, da bo računanje v metodi fit hitrejše. Moja implementacija potrebuje nekaj sekund.
md = MovieData('data/movies.dat')
uim = UserItemData('data/user_ratedmovies.dat', min_ratings=1000)
rp = ItemBasedPredictor()
rec = Recommender(rp)
rec.fit(uim)
print(uim.movies)
print("Podobnost med filmoma 'Men in black'(1580) in 'Ghostbusters'(2716): ", rp.similarity(1580, 2716))
print("Podobnost med filmoma 'Men in black'(1580) in 'Schindler's List'(527): ", rp.similarity(1580, 527))
print("Podobnost med filmoma 'Men in black'(1580) in 'Independence day'(780): ", rp.similarity(1580, 780))
Rezultat:
Podobnost med filmoma 'Men in black'(1580) in 'Ghostbusters'(2716): 0.233955231768 Podobnost med filmoma 'Men in black'(1580) in 'Schindler's List'(527): 0.0 Podobnost med filmoma 'Men in black'(1580) in 'Independence day'(780): 0.424661258447
Če uporabimo recommender za uporabnika 78, dobimo:
print("Predictions for 78: ")
rec_items = rec.recommend(78, n=15, rec_seen=False)
for idmovie, val in rec_items:
print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))
Film: Shichinin no samurai, ocena: 4.355734702553732 Film: The Usual Suspects, ocena: 4.354681671325777 Film: The Silence of the Lambs, ocena: 4.335305215729613 Film: Sin City, ocena: 4.278687059261374 Film: Monsters, Inc., ocena: 4.2175806874956665 Film: The Incredibles, ocena: 4.207098405922537 Film: The Lord of the Rings: The Fellowship of the Ring, ocena: 4.15279203714804 Film: Batman Begins, ocena: 4.146413660330134 Film: Die Hard, ocena: 4.125915501065452 Film: Rain Man, ocena: 4.071535187988423 Film: The Lord of the Rings: The Return of the King, ocena: 4.020237426179001 Film: A Beautiful Mind, ocena: 4.015142457113093 Film: Good Will Hunting, ocena: 4.009280780263334 Film: The Lord of the Rings: The Two Towers, ocena: 3.9414763181545847 Film: Indiana Jones and the Last Crusade, ocena: 3.7969765828982167
Najbolj podobni filmi (6)
Izpišite 20 parov najbolj podobnih filmov. Če spet uporabimo le filme, ki imajo vsaj 1000 ocen, dobimo:
Film1: The Lord of the Rings: The Return of the King, Film2: The Lord of the Rings: The Two Towers, podobnost: 0.8439842148481411 Film1: The Lord of the Rings: The Two Towers, Film2: The Lord of the Rings: The Return of the King, podobnost: 0.8439842148481411 Film1: The Lord of the Rings: The Two Towers, Film2: The Lord of the Rings: The Fellowship of the Ring, podobnost: 0.8231885401761887 Film1: The Lord of the Rings: The Fellowship of the Ring, Film2: The Lord of the Rings: The Two Towers, podobnost: 0.8231885401761887 Film1: The Lord of the Rings: The Return of the King, Film2: The Lord of the Rings: The Fellowship of the Ring, podobnost: 0.8079374897442487 Film1: The Lord of the Rings: The Fellowship of the Ring, Film2: The Lord of the Rings: The Return of the King, podobnost: 0.8079374897442487 Film1: Kill Bill: Vol. 2, Film2: Kill Bill: Vol. 2, podobnost: 0.7372340224381033 Film1: Kill Bill: Vol. 2, Film2: Kill Bill: Vol. 2, podobnost: 0.7372340224381033 Film1: Star Wars: Episode V - The Empire Strikes Back, Film2: Star Wars, podobnost: 0.7021321132220316 Film1: Star Wars, Film2: Star Wars: Episode V - The Empire Strikes Back, podobnost: 0.7021321132220316 Film1: The Mask, Film2: Ace Ventura: Pet Detective, podobnost: 0.6616471778494041 Film1: Ace Ventura: Pet Detective, Film2: The Mask, podobnost: 0.6616471778494041 Film1: Star Wars: Episode VI - Return of the Jedi, Film2: Star Wars: Episode V - The Empire Strikes Back, podobnost: 0.5992253753778951 Film1: Star Wars: Episode V - The Empire Strikes Back, Film2: Star Wars: Episode VI - Return of the Jedi, podobnost: 0.5992253753778951 Film1: Star Wars: Episode I - The Phantom Menace, Film2: Independence Day, podobnost: 0.5610426219249982 Film1: Independence Day, Film2: Star Wars: Episode I - The Phantom Menace, podobnost: 0.5610426219249982 Film1: Austin Powers: The Spy Who Shagged Me, Film2: Ace Ventura: Pet Detective, podobnost: 0.5546511205201548 Film1: Ace Ventura: Pet Detective, Film2: Austin Powers: The Spy Who Shagged Me, podobnost: 0.5546511205201548 Film1: Star Wars: Episode VI - Return of the Jedi, Film2: Star Wars, podobnost: 0.5537849318137374 Film1: Star Wars, Film2: Star Wars: Episode VI - Return of the Jedi, podobnost: 0.5537849318137374
Priporočanje glede na trenutno ogledano vsebino (7)
Kaj bi pokazali v kategoriji "Gledalci, ki so gledali A, so gledali tudi B"? Razredu ItemBasedPredictor dodajte metodo similarItems(self, item, n), ki vrne n najbolj podobnih filmov izbranemu filmu.
rec_items = rp.similarItems(4993, 10)
print('Filmi podobni "The Lord of the Rings: The Fellowship of the Ring": ')
for idmovie, val in rec_items:
print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))
Filmi podobni "The Lord of the Rings: The Fellowship of the Ring": Film: The Lord of the Rings: The Two Towers, ocena: 0.8231885401761887 Film: The Lord of the Rings: The Return of the King, ocena: 0.8079374897442487 Film: Star Wars: Episode V - The Empire Strikes Back, ocena: 0.23961943073496453 Film: Star Wars, ocena: 0.21965586527074088 Film: The Matrix, ocena: 0.2151555270688026 Film: Raiders of the Lost Ark, ocena: 0.19944276706345052 Film: The Usual Suspects, ocena: 0.18321188451910767 Film: Blade Runner, ocena: 0.16399681315410303 Film: Schindler's List, ocena: 0.16105905138148724 Film: Monty Python and the Holy Grail, ocena: 0.15780453798519137
Priporočilo zase (7)
Naredite še priporočilo zase z metodo "item-based"; izberite si cca. 20 filmov, ki jih poznate in jih ročno ocenite. Dodajte svoje ocene v movielens bazo in si priporočite 10 filmov.
Napovedovanje z metodo Slope one (7)
Napišite prediktor SlopeOnePredictor, ki izračuna napovedano vrednost ocen za produkte z metodo Slope one. Primer:
md = MovieData('data/movies.dat')
uim = UserItemData('data/user_ratedmovies.dat', min_ratings=1000)
rp = SlopeOnePredictor()
rec = Recommender(rp)
rec.fit(uim)
print("Predictions for 78: ")
rec_items = rec.recommend(78, n=15, rec_seen=False)
for idmovie, val in rec_items:
print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))
Rezultat:
Predictions for 78:
Film: The Usual Suspects, ocena: 4.325079182263173
Film: The Lord of the Rings: The Fellowship of the Ring, ocena: 4.155293229840448
Film: The Lord of the Rings: The Return of the King, ocena: 4.153135076202185
Film: The Silence of the Lambs, ocena: 4.127978169643881
Film: Shichinin no samurai, ocena: 4.119790444913598
Film: The Lord of the Rings: The Two Towers, ocena: 4.083325894849594
Film: Indiana Jones and the Last Crusade, ocena: 3.9670398355464194
Film: The Incredibles, ocena: 3.9664496674557546
Film: Good Will Hunting, ocena: 3.963362387354114
Film: Sin City, ocena: 3.942619137615212
Film: Batman Begins, ocena: 3.9375326640077017
Film: A Beautiful Mind, ocena: 3.9140940935239508
Film: Rain Man, ocena: 3.9107819079644943
Film: Monsters, Inc., ocena: 3.8819375978658006
Film: Finding Nemo, ocena: 3.8807711131654794