Collaborative recommendation algorithm to achieve -php

Overview:

  Because of the recent study carried out for this algorithm, so recently a similar Taobao commodity recommendation of collaborative recommendation algorithm were consolidated summary, this article will be implemented in php language, the following article will be terminated:  

  (1) What is the collaborative recommendation algorithm? What is the use?

  (2) The method according to any mathematical formula, why use it?

  (3) What is the process to achieve?

  (4) specific implementation steps.

 

  First of all, the first point, we need to implement an algorithm to know what use is it, what is the core idea is? As the name suggests, its purpose is to give us some similarities recommend, recommend your favorite Congratulations, you like and then recommended to you based on your friends and other forecasting; we give an example to summarize the core idea of the algorithm: the the core idea of the algorithm can be summarized as follows: If a, b like the same series of articles (temporarily called b is a neighbor of it), it is a likely like other items like b. Implementation process algorithm can be summarized as follows: 1. Identify a neighbor 2. What are predicting a neighbor which items might like 3. a recommendation to a possible favorite items.

  Now that we understand the core idea of ​​the future, we know that the core of the algorithm is a mathematical logic, we must understand its mathematical thinking, and then converted into programming ideas to achieve our goals:

       Let's recall the law of cosines:

 

 

  

  

  The smaller the angle the larger visible cosA cosA value, the side length of the side b and c are similar, so we use a vector-based manner Cosine Release following formula:

1. cosine similarity (request neighbor):

No

2. prediction formula (a forecast which goods might like):

No

 

  Next we introduce the implementation process:

 

 

 

 

  Specifically, we now take a look at how to use php to achieve this algorithm:

  (1) Data Preparation: Here we directly define a set of data

  

$userarray= [
    ['name'=>'A','a'=>3,'b'=>2,'c'=>1,'d'=>5,'e'=>null,'f'=>null,'g'=>null], 
    ['name'=>'B','a'=>1,'b'=>6,'c'=>6,'d'=>5,'e'=>2,'f'=>3,'g'=>5],
    ['name'=>'C','a'=>3,'b'=>5,'c'=>null,'d'=>4,'e'=>3,'f'=>3,'g'=>6],
    ['name'=>'D','a'=>4,'b'=>1,'c'=>1,'d'=>5,'e'=>3,'f'=>3,'g'=>3],
    ['name'=>'E','a'=>5,'b'=>1,'c'=>null,'d'=>4,'e'=>5,'f'=>1,'g'=>5],
    ['name'=>'F','a'=>1,'b'=>3,'c'=>2,'d'=>5,'e'=>6,'f'=>null,'g'=>4],
    ['name'=>'G','a'=>1,'b'=>5,'c'=>2,'d'=>5,'e'=>2,'f'=>3ll,'g'=>5]

                ];

 

 

 

   (2)我们以A用户为对象分别计算出他们的余弦相似度,然后将其存入数组$cos中:

  

/*
 * 以下示例只求A的推荐
 */
 $denominator_left = 0;//分母左边初始值
//开始计算cos
//计算分母左边
for($i=1;$i<8;$i++){
    if($userarray[0][$i] != null){//$userarray[0]代表A
        $denominator_left += $userarray[0][$i] * $userarray[0][$i];
    }
}
 
$denominator_left = sqrt($denominator_left);//取算数平方根的值
 
for($i=1;$i<6;$i++){
    $numerator = 0;//分子初始值
    $denominator_right = 0;//分母右边初始值
    
    for($j=1;$j<8;$j++){
        //计算分子
        if($userarray[0][$j] != null && $userarray[$i][$j] != null){
            $numerator += $userarray[0][$j] * $userarray[$i][$j];
        }
        //计算分母右边
        if($userarray[$i][$j] != null){
            $denominator_right += $userarray[$i][$j] * $userarray[$i][$j];
        }            
    }
     $denominator_right = sqrt($denominator_right );
$cos[$i]['cos'] = $numerator /$denominator_left /$denominator_right ;//存储当前用户的cos近似值

   $cos[$i]['name'] = $userarray[$i]['name'];//存储当前用户的名字
   $cos[$i]['e'] = $userarray[$i]['e'];//存储当前用户的e物品评分

   $cos[$i]['f'] = $userarray[$i]['f'];//存储当前用户的f物品评分
   $cos[$i]['g'] = $userarray[$i]['g'];//存储当前用户的g物品评分
 
 
 
 
 
}

  (3)我们对余弦近似值进行由大到小的排序,抽取前三个用户作为A用户的邻居,我们使用冒泡排序法对其进行排序

  

// 第一层可以理解为从数组中键为0开始循环到最后一个
for ($i = 0; $i < count($cos) ; $i++) {
  // 第二层为从$i+1的地方循环到数组最后
    for ($j = $i+1; $j < count($cos); $j++) {
     // 比较数组中两个相邻值的大小
        if ($cos[$i]['cos'] < $cos[$j]['cos']) {
            $tem = $cos[$i]; // 这里临时变量,存贮$i的值
            $cos[$i] = $cos[$j]; // 第一次更换位置
            $cos[$j] = $tem; // 完成位置互换
        }
    }        
}

 

  (4)接下来我们对A用户可能喜欢的物品进行预测,利用第二个公式,求出product值

  

//计算A对e的评分
$numerator= 0;//分子初始值
$denominator= 0;//分母初始值
for($i=0;$i<3;$i++){
    $numerator+= $cos[$i]['cos'] * $cos[$i]['e'];
    $denominator+= $cos[$i]['cos'];
}

$score_e = $numerator/sqrt($denominator);

 
//计算A对f的评分
$numerator= 0;//分子初始值
$denominator= 0;//分母初始值
for($i=0;$i<3;$i++){
    $numerator+= $cos[$i]['cos'] * $cos[$i]['f'];
    $denominator+= $cos[$i]['cos'];
}

$score_f= $numerator/sqrt($denominator);

//计算A对g的评分
$numerator= 0;//分子初始值
$denominator= 0;//分母初始值
for($i=0;$i<3;$i++){
    $numerator+= $cos[$i]['cos'] * $cos[$i]['g'];
    $denominator+= $cos[$i]['cos'];
}

$score_g= $numerator/sqrt($denominator);

  Finally, we can compare these values, you can select the maximum value of the items recommended to the user A

  

 

  

 

Guess you like

Origin www.cnblogs.com/zhangbobo/p/11577523.html