-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmln.py
45 lines (37 loc) · 1.74 KB
/
mln.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
from utility import read_mapping
from movielens.process import process_set
from sklearn import metrics
from math import exp
from statistics import mean, stdev
def mln(user, near_count, train_count, test_count, iterations):
users = read_mapping("./process/users.txt")
movies = read_mapping("./process/movies.txt")
near = read_mapping("./process/near.txt")[user][:near_count]
train = process_set(user=user, count=train_count, users=users, movies=movies, mechanism=None)
test = process_set(user=user, count=test_count, users=users, movies=movies, mechanism=None)
weights = [1 / near_count] * near_count
em(weights, train, users, near, iterations)
predictions = [predict(target[0], weights, users, near) for target in test]
targets = [1 if target[1] else 0 for target in test]
return metrics.roc_auc_score(targets, predictions)
def em(weights, train, users, near, iterations):
for _ in range(iterations):
predictions = [predict(target[0], weights, users, near) for target in train]
weights = [reweigh(train, users[user], predictions) for user in near]
total = sum(weights)
weights = [weight / total for weight in weights]
def predict(movie, weights, users, near):
score = 0
for weight, user in zip(weights, near):
score += weight if movie in users[user] else -weight
return 1 / (1 + exp(-score))
def reweigh(train, user, predictions):
weight = 0
for target, prediction in zip(train, predictions):
weight += prediction if target[0] in user else 1-prediction
return weight
USER = "Ariel-Garcé"
PATH = f"./experiment/mln-{USER}"
a = [mln(user=USER, near_count=5, train_count=100, test_count=100, iterations=10) for _ in range(10)]
print(mean(a))
print(stdev(a))