-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaselines.py
98 lines (81 loc) · 2.61 KB
/
baselines.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gzip
from collections import defaultdict
def readGz(f):
for l in gzip.open(f):
yield eval(l)
### Rating baseline: compute averages for each user, or return the global average if we've never seen the user before
allRatings = []
userRatings = defaultdict(list)
for l in readGz("train.json.gz"):
user,business = l['reviewerID'],l['itemID']
allRatings.append(l['rating'])
userRatings[user].append(l['rating'])
globalAverage = sum(allRatings) / len(allRatings)
userAverage = {}
for u in userRatings:
userAverage[u] = sum(userRatings[u]) / len(userRatings[u])
predictions = open("predictions_Rating.txt", 'w')
for l in open("pairs_Rating.txt"):
if l.startswith("reviewerID"):
#header
predictions.write(l)
continue
u,i = l.strip().split('-')
if u in userAverage:
predictions.write(u + '-' + i + ',' + str(userAverage[u]) + '\n')
else:
predictions.write(u + '-' + i + ',' + str(globalAverage) + '\n')
predictions.close()
### Would-purchase baseline: just rank which businesses are popular and which are not, and return '1' if a business is among the top-ranked
businessCount = defaultdict(int)
totalPurchases = 0
for l in readGz("train.json.gz"):
user,business = l['reviewerID'],l['itemID']
businessCount[business] += 1
totalPurchases += 1
mostPopular = [(businessCount[x], x) for x in businessCount]
mostPopular.sort()
mostPopular.reverse()
return1 = set()
count = 0
for ic, i in mostPopular:
count += ic
return1.add(i)
if count > totalPurchases/2: break
predictions = open("predictions_Purchase.txt", 'w')
for l in open("pairs_Purchase.txt"):
if l.startswith("reviewerID"):
#header
predictions.write(l)
continue
u,i = l.strip().split('-')
if i in return1:
predictions.write(u + '-' + i + ",1\n")
else:
predictions.write(u + '-' + i + ",0\n")
predictions.close()
### Category prediction baseline: Just consider some of the most common words from each category
catDict = {
"Women": 0,
"Men": 1,
"Girls": 2,
"Boys": 3,
"Baby": 4
}
predictions = open("predictions_Category.txt", 'w')
predictions.write("reviewerID-reviewHash,category\n")
for l in readGz("test_Category.json.gz"):
cat = catDict['Women'] # If there's no evidence, just choose the most common category in the dataset
words = l['reviewText'].lower()
if 'wife' in words:
cat = catDict['Women']
if 'husband' in words:
cat = catDict['Men']
if 'daughter' in words:
cat = catDict['Girls']
if 'son' in words:
cat = catDict['Boys']
if 'baby' in words:
cat = catDict['Baby']
predictions.write(l['reviewerID'] + '-' + l['reviewHash'] + "," + str(cat) + "\n")
predictions.close()