Advertisement
Mitrezzz

СНЗ Лаб.4 Табела на слични корисници

Dec 27th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import math
  2. oceniPoKorisnici={
  3.     'Lisa Rose': {'Catch Me If You Can': 3.0 , 'Snakes on a Plane': 3.5, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0, 'Snitch': 3.0},
  4.     'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 'Just My Luck': 1.5,  'The Night Listener': 3.0,'You, Me and Dupree': 3.5},
  5.     'Michael Phillips': {'Catch Me If You Can': 2.5, 'Lady in the Water': 2.5,'Superman Returns': 3.5, 'The Night Listener': 4.0, 'Snitch': 2.0},
  6.     'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,'The Night Listener': 4.5, 'Superman Returns': 4.0,'You, Me and Dupree': 2.5},
  7.     'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,'Just My Luck': 2.0, 'Superman Returns': 3.0, 'You, Me and Dupree': 2.0},
  8.     'Jack Matthews': {'Catch Me If You Can': 4.5, 'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5, 'Snitch': 4.5},
  9.     'Toby': {'Snakes on a Plane':4.5, 'Snitch': 5.0},
  10.     'Michelle Nichols': {'Just My Luck' : 1.0, 'The Night Listener': 4.5, 'You, Me and Dupree': 3.5, 'Catch Me If You Can': 2.5, 'Snakes on a Plane': 3.0},
  11.     'Gary Coleman': {'Lady in the Water': 1.0, 'Catch Me If You Can': 1.5, 'Superman Returns': 1.5, 'You, Me and Dupree': 2.0},
  12.     'Larry': {'Lady in the Water': 3.0, 'Just My Luck': 3.5, 'Snitch': 1.5, 'The Night Listener': 3.5}
  13.     }
  14. def sim_distance(prefs,person1,person2):
  15.     si={}
  16.     for item in prefs[person1]:
  17.         if item in prefs[person2]:
  18.             si[item]=1
  19.     if len(si)==0: return 0
  20.     sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
  21.     for item in prefs[person1] if item in prefs[person2]])
  22.     return round(1/(1+math.sqrt(sum_of_squares)),3)
  23. def sim_pearson(oceni,person1,person2):
  24.     si={}
  25.     for item in oceni[person1]:
  26.         if item in oceni[person2]:
  27.             si[item] = 1
  28.     n = len(si)
  29.     if n==0:
  30.         return 0
  31.     sum1=sum([oceni[person1][it] for it in si])
  32.     sum2=sum([oceni[person2][it] for it in si])
  33.     sum1Sq=sum([pow(oceni[person1][it],2) for it in si])
  34.     sum2Sq=sum([pow(oceni[person2][it],2) for it in si])
  35.     pSum=sum([oceni[person1][it]*oceni[person2][it] for it in si])
  36.     num=pSum-(sum1*sum2/n)
  37.     den=math.sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))
  38.     if den==0: return 0
  39.     r=num/den
  40.     return round(r,3)
  41. def sim (oceni,person1, person2):
  42.     numb=0
  43.     for item in oceni[person1]:
  44.         if item in oceni[person2]:
  45.            numb+=1
  46.     return numb
  47. def TabelaNaSlicniKorisnici(oceni):
  48.     slicnosti={}
  49.     for p1 in oceni:
  50.         for p2 in oceni:
  51.             slicnosti.setdefault(p1,{})
  52.             slicnosti[p1][p2] = (sim_distance(oceni,p1,p2),sim_pearson(oceni,p1,p2),sim(oceni,p1,p2))
  53.     return slicnosti
  54. if __name__ == "__main__":
  55.     korisnik1=input()
  56.     korisnik2=input()
  57.     # korisnik1='Mick LaSalle'
  58.     # korisnik2='Lisa Rose'
  59.     # print oceniPoKorisnici
  60.     tabela=TabelaNaSlicniKorisnici(oceniPoKorisnici)
  61.     # print tabela
  62.     print tabela[korisnik1][korisnik2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement