Advertisement
Guest User

3

a guest
Nov 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. oceniPoKorisnici={
  5. '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},
  6. '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},
  7. '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},
  8. '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},
  9. '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},
  10. '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},
  11. 'Toby': {'Snakes on a Plane':4.5, 'Snitch': 5.0},
  12. '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},
  13. '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},
  14. 'Larry': {'Lady in the Water': 3.0, 'Just My Luck': 3.5, 'Snitch': 1.5, 'The Night Listener': 3.5}
  15. }
  16.  
  17. def sim_distance(oceni,person1,person2):
  18. zaednicki = {}
  19. zaedn = 0
  20. for film in oceni[person1].keys():
  21. if film in oceni[person2]:
  22. zaednicki[film] = 1
  23. zaedn+=1
  24.  
  25. if len(zaednicki) == 0:
  26. return 0
  27.  
  28. zbirKvadrati = 0;
  29. for film in zaednicki.keys():
  30. zbirKvadrati += (oceni[person1][film] - oceni[person2][film])**2
  31.  
  32. slicnost = round(1/(1 + math.sqrt(zbirKvadrati)),3)
  33.  
  34. return slicnost
  35.  
  36. def sim_pearson(oceni,person1,person2):
  37. zaednicki = {}
  38. zaedn = 0
  39. for film in oceni[person1].keys():
  40. if film in oceni[person2]:
  41. zaednicki[film] = 1
  42. zaedn+=1
  43.  
  44. if len(zaednicki) == 0:
  45. return 0
  46.  
  47. sumaOceni1 = 0
  48. sumaOceni2 = 0
  49. sumaOceniKv1 = 0
  50. sumaOceniKv2 = 0
  51. sumaZaednPr = 0
  52.  
  53. for film in zaednicki.keys():
  54. ocena1 = oceni[person1][film]
  55. ocena2 = oceni[person2][film]
  56. sumaOceni1 += ocena1
  57. sumaOceni2 += ocena2
  58. sumaOceniKv1 += ocena1**2
  59. sumaOceniKv2 += ocena2**2
  60. sumaZaednPr += ocena1 * ocena2
  61.  
  62. broitel = zaedn * sumaZaednPr - sumaOceni1 * sumaOceni2
  63. imenitel = math.sqrt(zaedn * sumaOceniKv1 - (sumaOceni1**2)) * math.sqrt(zaedn * sumaOceniKv2 - sumaOceni2**2)
  64. if imenitel==0:
  65. if broitel>0:
  66. return 1
  67. elif broitel<0:
  68. return -1
  69. else:
  70. return -2
  71.  
  72. formula = broitel / imenitel
  73. f = round(formula,3)
  74. return f,zaedn
  75.  
  76.  
  77.  
  78.  
  79. korisnik1 = input()
  80. korisnik2 = input()
  81. # korisnik1='Mick LaSalle'
  82. # korisnik2='Lisa Rose'
  83. print '({},'.format(sim_distance(oceniPoKorisnici,korisnik1, korisnik2)),
  84. print '{}, {})'.format(*sim_pearson(oceniPoKorisnici, korisnik1, korisnik2)),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement