-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinclude.jl
More file actions
61 lines (54 loc) · 1.63 KB
/
include.jl
File metadata and controls
61 lines (54 loc) · 1.63 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
function RecWalk(TrainSet, ItemModel, α=0.01)
n,m = size(TrainSet)
Muu = speye(n)
Mii = RowStochastic(ItemModel,"dmax")
Hui = RowStochastic(TrainSet)
Hiu = RowStochastic(TrainSet')
H = vcat(hcat(spzeros(n,n),Hui), hcat(Hiu,spzeros(m,m)))
M = vcat(hcat(Muu,spzeros(n,m)), hcat(spzeros(m,n),Mii))
P = α*H+(1-α)*M
return P
end
function RowStochastic(A, strategy="standard")
if strategy == "dmax"
row_sums = sum(A, 2)
dmax = maximum(row_sums)
A_temp = (1.0 / dmax) * A
return (I - spdiagm(vec(sum(A_temp, 2)))) + A_temp
else
row_sums = sum(A, 2)
row_sums[row_sums .== 0] = 1 # replacing the zero row sums with 1
return spdiagm(vec(1 ./ row_sums)) * A
end
end
function readItemModel(filename, m)
A = readdlm(filename, skipblanks = false)
A[A .== ""] = 0; A = Array{Float64}(A)
s1, s2 = size(A)
rows = []; cols = []; vals = Float64[];
for i = 1:s1
items = Array{Int64}(A[i,1:2:s2 - 1])
indx = find(items .> 0)
items = items[indx]
scores = A[i,2 * indx]
append!(rows, i * ones(length(items)))
append!(cols, items)
append!(vals, scores)
end
if maximum(rows) < m || maximum(cols) < m
append!(rows, [m])
append!(cols, [m])
append!(vals, [0])
end
return sparse(rows, cols, vals)
end
@everywhere function Single_HR_RR_NDCG(Π, T, K)
target = Π[T[1]]
pos = sum(Π[T].>=target)
if 1 <= pos <= K
HR = 1; RR = 1 / pos; NDCG = 1/log2(pos+1)
else
HR = 0; RR = 0; NDCG = 0
end
return HR, RR, NDCG
end