2011-03-27 89 views
2

我翻译以下SVD recommendation system,用Ruby编写的,分配问题数学:数学SVD推荐系统,内环路

require 'linalg' 

users = { 1 => "Ben", 2 => "Tom", 3 => "John", 4 => "Fred" } 
m = Linalg::DMatrix[ 
      #Ben, Tom, John, Fred 
      [5,5,0,5], # season 1 
      [5,0,3,4], # season 2 
      [3,4,0,3], # season 3 
      [0,0,5,3], # season 4 
      [5,4,4,5], # season 5 
      [5,4,5,5] # season 6 
      ] 

# Compute the SVD Decomposition 
u, s, vt = m.singular_value_decomposition 
vt = vt.transpose 

# Take the 2-rank approximation of the Matrix 
# - Take first and second columns of u (6x2) 
# - Take first and second columns of vt (4x2) 
# - Take the first two eigen-values (2x2) 
u2 = Linalg::DMatrix.join_columns [u.column(0), u.column(1)] 
v2 = Linalg::DMatrix.join_columns [vt.column(0), vt.column(1)] 
eig2 = Linalg::DMatrix.columns [s.column(0).to_a.flatten[0,2], s.column(1).to_a.flatten[0,2]] 

# Here comes Bob, our new user 
bob = Linalg::DMatrix[[5,5,0,0,0,5]] 
bobEmbed = bob * u2 * eig2.inverse 

# Compute the cosine similarity between Bob and every other User in our 2-D space 
user_sim, count = {}, 1 
v2.rows.each { |x| 
    user_sim[count] = (bobEmbed.transpose.dot(x.transpose))/(x.norm * bobEmbed.norm) 
    count += 1 
    } 

# Remove all users who fall below the 0.90 cosine similarity cutoff and sort by similarity 
similar_users = user_sim.delete_if {|k,sim| sim < 0.9 }.sort {|a,b| b[1] <=> a[1] } 
similar_users.each { |u| printf "%s (ID: %d, Similarity: %0.3f) \\n", users[u[0]], u[0], u[1] } 

# We'll use a simple strategy in this case: 
# 1) Select the most similar user 
# 2) Compare all items rated by this user against your own and select items that you have not yet rated 
# 3) Return the ratings for items I have not yet seen, but the most similar user has rated 
similarUsersItems = m.column(similar_users[0][0]-1).transpose.to_a.flatten 
myItems = bob.transpose.to_a.flatten 

not_seen_yet = {} 
myItems.each_index { |i| 
    not_seen_yet[i+1] = similarUsersItems[i] if myItems[i] == 0 and similarUsersItems[i] != 0 
} 

printf "\\n %s recommends: \\n", users[similar_users[0][0]] 
not_seen_yet.sort {|a,b| b[1] <=> a[1] }.each { |item| 
    printf "\\tSeason %d .. I gave it a rating of %d \\n", item[0], item[1] 
} 

print "We've seen all the same seasons, bugger!" if not_seen_yet.size == 0 

以下是相应的Mathematica代码:

Clear[s, u, v, s2, u2, v2, m, n, testdata, trainingdata, user, user2d]; 
find1nn[trainingdata_, user_] := { 
    {u , s, v} = SingularValueDecomposition[Transpose[trainingdata]]; 
    (* Reducr to 2 dimensions. *) 
    u2 = u[[All, {1, 2}]]; 
    s2 = s[[{1, 2}, {1, 2}]]; 
    v2 = v[[All, {1, 2}]]; 
    user2d = user.u2.Inverse[s2]; 
    {m, n} = Dimensions[v2]; 
    closest = -1; 
    index = -1; 
    For[a = 1, a < m, a++, 
    {distance = 1 - CosineDistance[v2[[a, {1, 2}]], user2d];, 
     If[distance > closest, {closest = distance, index = a}];}]; 
    closestuserratings = trainingdata[[index]]; 
    closestuserratings 
    } 
rec[closest_, userx_] := { 
    d = Dimensions[closest]; 
    For[b = 1, b <= d[[2]], b++, 
    If[userx[[b]] == 0., userx[[b]] = closest[[1, b]]] 
    ] 
    userx 
    } 
finalrec[td_, user_] := rec[find1nn[td, user], user] 
(*Clear[s,u,v,s2,u2,v2,m,n,testdata,trainingdata,user,user2d]*) 
testdata = {{5., 5., 3., 0., 5., 5.}, {5., 0., 4., 1., 4., 4.}, {0., 
    3., 0., 5., 4., 5.}, {5., 4., 3., 3., 5., 5.}}; 
bob = {5., 0., 4., 0., 4., 5.}; 
(*recommend[testdata,bob]*) 
find1nn[testdata, bob] 
finalrec[testdata, bob] 

对于某些原因,它没有分配用户内部的索引,但在外面。什么可能导致这种情况发生?

+0

您可以编辑你的问题,只是张贴原来的片段?如果该链接断开,则此问题的_entire_上下文将丢失。 – 2011-03-27 12:40:48

+0

我厌倦了我自己的翻译,但是我得到的http://farm1.static.flickr.com/133/358494623_db22603640_o.png的标志不匹配。什么会造成这种情况? – 2011-03-27 23:31:12

+0

@Mr。奇数的错误,加起来来自这两个部分。 – 2011-03-28 04:41:12

回答

3

请查阅数学文档中变量的本地化教程。问题在于你的rec功能。问题在于你无法正常修改Mathematica中的输入变量(如果你的函数有一个保持属性,那么你可以做到这一点,以便将所讨论的参数传递给它未评估,但事实并非如此这里):

rec[closest_, userxi_] := 
Block[{d, b, userx = userxi}, {d = Dimensions[closest]; 
    For[b = 1, b <= d[[2]], b++, 
    If[userx[[b]] == 0., userx[[b]] = closest[[1, b]]]]; 
    userx} 
1

没有试图了解你想要达到的,在这里你有一个更Mathematca十岁上下,但相当于(我希望)工作的代码。

显式循环都没有了,而且很多不必要的瓦尔消除。所有变量现在都是本地的,所以不需要使用Clear []。

find1nn[trainingdata_, user_] := 
    Module[{u, s, v, v2, user2d, m, distances}, 
    {u, s, v} = SingularValueDecomposition[Transpose[trainingdata]]; 
    v2 = v[[All, {1, 2}]]; 
    user2d = user.u[[All, {1, 2}]].Inverse[s[[{1, 2}, {1, 2}]]]; 
    m = [email protected][v2]; 
    distances = (1 - CosineDistance[v2[[#, {1, 2}]], user2d]) & /@ Range[m - 1]; 
    {trainingdata[[Ordering[distances][[-1]]]]}]; 

rec[closest_, userxi_] := userxi[[#]] /. {0. -> closest[[1, #]]} & /@ 
          Range[Dimensions[closest][[2]]]; 

finalrec[td_, user_] := rec[find1nn[td, user], user]; 

我相信它仍然可以优化了不少。

+1

是的,位置[距离,#] [[1,1]]和@Max [距离]可以写成Ordering [距离] [[ - 1]],它更加简洁和快30%。 – 2011-03-27 20:12:15

+0

@Sjoerd完成。用它编辑(还有一些)mods。谢谢! – 2011-03-27 20:22:20

+0

您遗留了一段旧代码挥之不去 – 2011-03-27 20:26:20

1

这是我在这个镜头基于贝利萨留的代码,并与Sjoerd的改进。

find1nn[trainingdata_, user_] := 
    Module[{u, s, v, user2d, distances}, 
    {u, s, v} = SingularValueDecomposition[trainingdata\[Transpose], 2]; 
    user2d = user . u . [email protected]; 
    distances = # ~CosineDistance~ user2d & /@ [email protected]; 
    trainingdata[[ distances ~Ordering~ 1 ]] 
    ] 

rec[closest_, userxi_] := If[# == 0, #2, #] & ~MapThread~ {userxi, closest[[1]]} 
+0

+1订购[距离,-1]而不是订购[距离] [[-1]]。这仍然更快。 – 2011-03-27 22:06:19

+0

向导我们不能只放弃1 - CosineDistance的1-部分并测试_smallest_值(Ordering [距离,1])而不是最大值? – 2011-03-27 22:09:17

+0

@Sjoerd,我不知道,我得考虑一下。但我还有一个担忧:'Ordering'只会返回一个位置,但'Position'可能会返回多个结果。由于我没有理解这是怎么回事,我不知道这是否是一个问题。我现在需要查看原始代码。 – 2011-03-27 22:39:33

0
Clear[s, u, v, s2, u2, v2, m, n, testdata, trainingdata, user, user2d]; 
recommend[trainingdata_, user_] := { 
    {u , s, v} = SingularValueDecomposition[Transpose[trainingdata]]; 
    (* Reducera till 2 dimensioner. *) 
    u2 = u[[All, {1, 2}]]; 
    s2 = s[[{1, 2}, {1, 2}]]; 
    v2 = v[[All, {1, 2}]]; 
    user2d = user.u2.Inverse[s2]; 
    {m, n} = Dimensions[v2]; 
    closest = -1; 
    index = -1; 
    For[a = 1, a < m, a++, 
    {distance = 1 - CosineDistance[v2[[a, {1, 2}]], user2d];, 
     If[distance > closest, {closest = distance, index = a}];}]; 
    closestuserratings = trainingdata[[index]]; 
    d = Dimensions[closestuserratings]; 
    updateduser = Table[0, {i, 1, d[[1]]}]; 
    For[b = 1, b <= d[[1]], b++, 
    If[user[[b]] == 0., updateduser[[b]] = closestuserratings[[b]], 
    updateduser[[b]] = user[[b]]] 
    ] 
    updateduser 
    } 
testdata = {{5., 5., 3., 0., 5., 5.}, {5., 0., 4., 1., 4., 4.}, {0., 
    3., 0., 5., 4., 5.}, {5., 4., 3., 3., 5., 5.}}; 
bob = {5., 0., 4., 0., 4., 5.}; 
recommend[testdata, bob] 

{{5。空,空为0,4空,1空,空4,5,空}}

现在的作品,但为什么空值?

+0

这是因为你错过了一个;在For循环之后。没有冒犯的意思,但你看过其他的贡献吗?这个问题已经得到解答,导致代码大大改进。 – 2011-03-29 14:12:30