2014-05-03 160 views
-1

大家好, 我想建立一個非常簡單的推薦系統。我無法想象背後的僞碼,但我知道邏輯。簡單的推薦系統

約翰購買A B C dé 亞歷購買A B X Y Z^

我買A B C由於F R

我會推薦項目從約翰,因爲我跟他有更多的比賽。因此,建議項目D和E.這很簡單。有人能幫助我嗎?

回答

1

這是蠻力的方式來做到這一點。也許不是最優雅的,但它的工作原理:

// $for = the user we are getting rec's for 
// $user_array = the list of all users 
function recommendations($for, $user_array) { 

    // For storing the best matching user 
    $best_match = array('name'=>'', 'count'=>0); 

    foreach ($user_array as $name=>$items) { 

     // If this user, skip 
     if ($name==$for) continue; 

     // Number of matching records 
     $c = count(array_intersect($items, $user_array[$for])); 

     // If better than the current best match, replace 
     if ($c > $best_match['count']) { 
      $best_match = array('name'=>$name, 'count'=>$c); 
     } 
    } 

    // If no matches found, return false 
    if ($best_match['count']==0) 
     return false; 

    // Return array of recommendations 
    return array_diff($user_array[$best_match['name']], $user_array[$for]); 
} 

使用範例:

$users = array (
    'john' => array('a', 'b', 'c', 'd', 'e'), 
    'alex' => array('a', 'b', 'x', 'y', 'z'), 
    'me' => array('a', 'b', 'c', 'f', 'r') 
); 

print_r(recommendations('me', $users)); 
+0

對於使用'continue'而不是'big''if'語句的+1。無論如何,如果你的代碼可以使用一個對象(用於比較'$ thisUser-> id'而不是僅僅使用一個名字,這對兩個用戶來說可能是相同的)並且訪問像'$ thisUser-> name'這樣的數據會更好。 ,'$ thisUser-> lastItemBought'等等。然而它開發了這個想法。 –

+0

@AlejandroIván是的,它肯定有一些弱點......用面向對象的方式使用肯定會更好,使用字符串「name」是一種可怕的方式來做到這一點...一個id是非常必要的。 –

+0

這裏$ user_array [$ best_match ['name']]的用法是什麼? –

0

推薦系統可以使用不同的算法,如基於用戶的基於項目,基於內容等techniques.I建造一般去用於實現少量算法和縮放找出最好的算法。例如,如果我們使用協作過濾技術和關聯規則,我們會在確定產品/類別建議時找到信心,提升和支持。 market basket analysis for recommendations