-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathratings.py
More file actions
60 lines (51 loc) · 2.03 KB
/
ratings.py
File metadata and controls
60 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
class ratings(object):
"""ratings"""
def __init__(self, inputdict):
self._data = inputdict
self.get_person_ids()
self.get_paper_ids()
self.get_ratings_array()
def get_person_ids(self):
self.persons = self._data.keys()
self.persons.sort()
def get_paper_ids(self):
self.papers = []
for person in self.persons:
for paper in self._data[person].keys():
if paper not in self.papers:
self.papers.append(paper)
self.papers.sort()
def get_ratings_array(self):
self.myratings = np.zeros((len(self.persons),len(self.papers)), dtype = float)
for person in self.persons:
for paper in self._data[person].keys():
self.myratings[self.persons.index(person),self.papers.index(paper)] = self._data[person][paper]
def compare_ratings(self, person1, person2):
# only check non-zero ratings
indices = np.where(self.myratings[self.persons.index(person1)] != 0) or np.where(self.myratings[self.persons.index(person2)] != 0)
if len(indices) == 0: return len(self.papers)*25+1 #5 is max difference;no basis to compare
cmp1 = self.myratings[self.persons.index(person1)][indices]
cmp2 = self.myratings[self.persons.index(person2)][indices]
return np.linalg.norm(cmp1 - cmp2)
def recommendations(self):
allrecommendations = {}
for person in self.persons:
minimum = len(self.papers)*25+1
notread = np.array([])
for otherperson in self.persons:
if otherperson == person: continue;
tmp = self.compare_ratings(person, otherperson)
if tmp < minimum:
closest = otherperson
minimum = tmp
notread = np.where(np.array(self.myratings[self.persons.index(otherperson)] == 0))[0]
if len(notread) == 0:
allrecommendations[person] = "No recommendation"
else:
maxindex = np.where(self.myratings[self.persons.index(otherperson)][notread] == self.myratings[self.persons.index(otherperson)][notread].max())
allrecommendations[person] = self.papers[int(maxindex[0])]
return allrecommendations
# Alternative distances:
# pearson
# tanimoto scipy.spatial.distance.rogerstanimoto