Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. oceniPoKorisnici = {
  2. 'Lisa Rose': {'Catch Me If You Can': 3.0, 'Snakes on a Plane': 3.5, 'Superman Returns': 3.5,
  3. '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,
  5. 'You, Me and Dupree': 3.5},
  6. 'Michael Phillips': {'Catch Me If You Can': 2.5, 'Lady in the Water': 2.5, 'Superman Returns': 3.5,
  7. '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,
  9. 'You, Me and Dupree': 2.5},
  10. 'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0,
  11. 'You, Me and Dupree': 2.0},
  12. 'Jack Matthews': {'Catch Me If You Can': 4.5, 'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
  13. 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5, 'Snitch': 4.5},
  14. 'Toby': {'Snakes on a Plane': 4.5, 'Snitch': 5.0},
  15. 'Michelle Nichols': {'Just My Luck': 1.0, 'The Night Listener': 4.5, 'You, Me and Dupree': 3.5,
  16. 'Catch Me If You Can': 2.5, 'Snakes on a Plane': 3.0},
  17. 'Gary Coleman': {'Lady in the Water': 1.0, 'Catch Me If You Can': 1.5, 'Superman Returns': 1.5,
  18. 'You, Me and Dupree': 2.0},
  19. 'Larry': {'Lady in the Water': 3.0, 'Just My Luck': 3.5, 'Snitch': 1.5, 'The Night Listener': 3.5}
  20. }
  21.  
  22. from math import sqrt
  23.  
  24. def sim_distance(oceni, person1, person2):
  25. # Se pravi lista na zaednicki predmeti (filmovi)
  26. zaednicki={}
  27. for item in oceni[person1].keys():
  28. for item in oceni[person2]:
  29. zaednicki.add(item)
  30.  
  31.  
  32. if len(zaednicki) == 0:
  33. return 0
  34. # # Soberi gi kvadratite na zaednickite razliki
  35. suma = 0.0
  36. for item in zaednicki:
  37. ocena1 = oceni[person1][item]
  38. ocena2 = oceni[person2][item]
  39. suma += (ocena1 - ocena2) ** 2
  40. # print(item, person1, ocena1, person2, ocena2)
  41.  
  42. result =1 / (1 + sqrt(suma))
  43. return (round(result,3),len(zaednicki))
  44.  
  45. def sim_pearson(oceni, person1, person2):
  46. # Se kreira recnik vo koj ke se cuvaat predmetite (filmovi) koi se oceneti od dvajcata
  47. # Vo recnikot ni se vazni samo klucevite za da gi cuvame iminjata na filmovite koi se zaednicki, a vrednostite ne ni se vazni
  48. zaednicki = {}
  49. for item in oceni[person1]:
  50. if item in oceni[person2]:
  51. zaednicki.add(item)
  52.  
  53. # Se presmetuva brojot na predmeti oceneti od dvajcata
  54. n = len(zaednicki)
  55.  
  56. # Ako nemaat zaednicki predmeti vrati korelacija 0
  57. if n == 0:
  58. return 0
  59.  
  60. # Soberi gi zaednickite oceni (rejtinzi) za sekoja licnost posebno
  61. sum1 = 0
  62. sum2 = 0
  63.  
  64. # Soberi gi kvadratite od zaednickite oceni (rejtinzi) za sekoja licnost posebno
  65. sum1Sq = 0
  66. sum2Sq = 0
  67.  
  68. # Soberi gi proizvodite od ocenite na dvete licnosti
  69. pSum = 0
  70. for item in zaednicki:
  71. ocena1 = oceni[person1][item]
  72. ocena2 = oceni[person2][item]
  73. sum1 += ocena1
  74. sum1Sq += ocena1 ** 2
  75. sum2 += ocena2
  76. sum2Sq += ocena2 ** 2
  77. pSum += ocena1 * ocena2
  78.  
  79. # Presmetaj go koeficientot na korelacija
  80. num = pSum - (sum1 * sum2 / n)
  81. den = sqrt((sum1Sq - pow(sum1, 2) / n) * (sum2Sq - pow(sum2, 2) / n))
  82. if den == 0:
  83. return 0
  84. r = num / den
  85. return (round(r, 3),n)
  86.  
  87.  
  88. if __name__ == "__main__":
  89.  
  90. person1=input()
  91. person2=input()
  92. print (sim_pearson(oceniPoKorisnici,person1,person2))
  93. print (sim_distance(oceniPoKorisnici,person1,person2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement