-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrneighbours.py
More file actions
55 lines (46 loc) · 1.45 KB
/
corrneighbours.py
File metadata and controls
55 lines (46 loc) · 1.45 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
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
from __future__ import print_function
from all_correlations import all_correlations
import numpy as np
from scipy import sparse
from load_ml100k import load
reviews = load()
def estimate_user(user, rest):
bu = user > 0
br = rest > 0
ws = all_correlations(bu, br)
selected = ws.argsort()[-100:]
estimates = rest[selected].mean(0)
estimates /= (.1 + br[selected].mean(0))
return estimates
def train_test(user, rest):
estimates = estimate_user(user, rest)
bu = user > 0
br = rest > 0
err = estimates[bu] - user[bu]
null = rest.mean(0)
null /= (.1 + br.mean(0))
nerr = null[bu] - user[bu]
return np.dot(err, err), np.dot(nerr, nerr)
def cross_validate_all():
err = []
for i in xrange(reviews.shape[0]):
err.append(
train_test(reviews[i], np.delete(reviews, i, 0))
)
revs = (reviews > 0).sum(1)
err = np.array(err)
rmse = np.sqrt(err / revs[:, None])
print(np.mean(rmse, 0))
print(np.mean(rmse[revs > 60], 0))
def all_estimates(reviews):
reviews = reviews.toarray()
estimates = np.zeros_like(reviews)
for i in xrange(reviews.shape[0]):
estimates[i] = estimate_user(reviews[i], np.delete(reviews, i, 0))
return estimates