Home  >  Article  >  Backend Development  >  Can PHP not use the recommendation algorithm?

Can PHP not use the recommendation algorithm?

angryTom
angryTomOriginal
2019-11-02 09:28:544552browse

Can PHP not use the recommendation algorithm?

#php can’t use the recommendation algorithm?

Recommendation algorithms are very old, and there were needs and applications before machine learning emerged.

Collaborative Filtering (Collaborative Filtering) is the most classic type of recommendation algorithm, including online collaboration and offline filtering. The so-called online collaboration is to find items that users may like through online data, while offline filtering is to filter out some data that are not worthy of recommendation, such as data with low recommendation value, or data that users have purchased despite high recommendation value. .

The following will introduce how to use PHP MySQL to implement a simple collaborative filtering algorithm.

To implement the collaborative filtering recommendation algorithm, we must first understand the core idea and process of the algorithm. The core idea of ​​this algorithm can be summarized as follows: if a and b like the same series of items (let’s call b a neighbor for now), then a is likely to like other items that b likes. The implementation process of the algorithm can be simply summarized as follows: 1. Determine which neighbors a has. 2. Use the neighbors to predict what kind of items a may like. 3. Recommend the items a may like to a.

The core formula of the algorithm is as follows:

1. Cosine similarity (finding neighbors):

Can PHP not use the recommendation algorithm?

2. Prediction formula (predict a What kind of items may you like):

Can PHP not use the recommendation algorithm?

From these two formulas alone, we can see that just calculating according to these two formulas requires A large number of loops and judgments are performed, and it also involves sorting issues, which involves the selection and use of sorting algorithms. Here we choose quick sort.

First create a table:

DROP TABLE IF EXISTS `tb_xttj`;
CREATE TABLE `tb_xttj` (
  `name` varchar(255) NOT NULL,
  `a` int(255) default NULL,
  `b` int(255) default NULL,
  `c` int(255) default NULL,
  `d` int(255) default NULL,
  `e` int(255) default NULL,
  `f` int(255) default NULL,
  `g` int(255) default NULL,
  `h` int(255) default NULL,
  PRIMARY KEY  (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
INSERT INTO `tb_xttj` VALUES ('John', '4', '4', '5', '4', '3', '2', '1', null);
INSERT INTO `tb_xttj` VALUES ('Mary', '3', '4', '4', '2', '5', '4', '3', null);
INSERT INTO `tb_xttj` VALUES ('Lucy', '2', '3', null, '3', null, '3', '4', '5');
INSERT INTO `tb_xttj` VALUES ('Tom', '3', '4', '5', null, '1', '3', '5', '4');
INSERT INTO `tb_xttj` VALUES ('Bill', '3', '2', '1', '5', '3', '2', '1', '1');
INSERT INTO `tb_xttj` VALUES ('Leo', '3', '4', '5', '2', '4', null, null, null);

Can PHP not use the recommendation algorithm?

Here we only recommend Leo in the last row to see which one of f, g, and h can be recommended to him.

Use php mysql, the flow chart is as follows:

Can PHP not use the recommendation algorithm?

The code to connect to the database and store it as a two-dimensional array is as follows:

header("Content-Type:text/html;charset=utf-8");
 
mysql_connect("localhost","root","admin");
mysql_select_db("geodatabase");
mysql_query("set names 'utf8'");
 
$sql = "SELECT * FROM tb_xttj";
$result = mysql_query($sql);
 
$array = array();
while($row=mysql_fetch_array($result))
{
$array[]=$row;//$array[][]是一个二维数组
}

Question 1: This step can be regarded as a whole table query. This kind of query is taboo. It is okay for such a small demonstration system, but it is inefficient for a big data system.

The code to find the Cos value of Leo and others is as follows:

/*
 * 以下示例只求Leo的推荐,如此给变量命名我也是醉了;初次理解算法,先不考虑效率和逻辑的问题,主要把过程做出来
 */
 
$cos = array();
$cos[0] = 0;
$fm1 = 0;
//开始计算cos
//计算分母1,分母1是第一个公式里面 “*”号左边的内容,分母二是右边的内容
for($i=1;$i<9;$i++){
if($array[5][$i] != null){//$array[5]代表Leo
$fm1 += $array[5][$i] * $array[5][$i];
}
}
 
$fm1 = sqrt($fm1);
 
for($i=0;$i<5;$i++){
$fz = 0;
$fm2 = 0;
echo "Cos(".$array[5][0].",".$array[$i][0].")=";
for($j=1;$j<9;$j++){
    //计算分子
if($array[5][$j] != null && $array[$i][$j] != null){
$fz += $array[5][$j] * $array[$i][$j];
}
//计算分母2
if($array[$i][$j] != null){
$fm2 += $array[$i][$j] * $array[$i][$j];
}
}
$fm2 = sqrt($fm2);
$cos[$i] = $fz/$fm1/$fm2;
echo $cos[$i]."<br/>";
}

The result obtained in this step:

Can PHP not use the recommendation algorithm?

will be asked For good Cos value sorting, the quick sort code is as follows:

//对计算结果进行排序,凑合用快排吧先
function quicksort($str){
if(count($str)<=1) return $str;//如果个数不大于一,直接返回
$key=$str[0];//取一个值,稍后用来比较;
$left_arr=array();
$right_arr=array();
for($i=1;$i<count($str);$i++){//比$key大的放在右边,小的放在左边;
if($str[$i]>=$key)
$left_arr[]=$str[$i];
else
$right_arr[]=$str[$i];
}
$left_arr=quicksort($left_arr);//进行递归;
$right_arr=quicksort($right_arr);
return array_merge($left_arr,array($key),$right_arr);//将左中右的值合并成一个数组;
}
 
$neighbour = array();//$neighbour只是对cos值进行排序并存储
$neighbour = quicksort($cos);

The $neighbour array here only stores the Cos values ​​sorted from large to small, and is not associated with people. This problem still needs to be solved.

Select the 3 people with the highest Cos values ​​as Leo’s neighbors:

//$neighbour_set 存储最近邻的人和cos值
$neighbour_set = array();
for($i=0;$i<3;$i++){
for($j=0;$j<5;$j++){
if($neighbour[$i] == $cos[$j]){
$neighbour_set[$i][0] = $j;
$neighbour_set[$i][1] = $cos[$j];
$neighbour_set[$i][2] = $array[$j][6];//邻居对f的评分
$neighbour_set[$i][3] = $array[$j][7];//邻居对g的评分
$neighbour_set[$i][4] = $array[$j][8];//邻居对h的评分
}
}
}
print_r($neighbour_set);
echo "<p><br/>";

The result of this step:

Can PHP not use the recommendation algorithm?This is a two-dimensional Array, the subscripts of the first level of the array are 0, 1, 2, representing 3 people. The second-level subscript 0 represents the order of the neighbors in the data table, for example, Jhon is the 0th person in the table; the subscript 1 represents the Cos value of Leo and the neighbor; the subscript 2, 3, and 4 represent the neighbor pair f and g respectively. , h rating.

Start prediction, and calculate the Predict code as follows:

Calculate Leo's predicted values ​​for f, g, h respectively. There is a problem here, that is, how to deal with it if some neighbors have empty scores for f, g, h. For example, Jhon and Mary's ratings for h are empty. Instinctively I thought of using if to judge, and if it is empty, skip this set of calculations, but whether this is reasonable remains to be considered. The following code does not write this if judgment.

//计算Leo对f的评分
$p_arr = array();
$pfz_f = 0;
$pfm_f = 0;
for($i=0;$i<3;$i++){
$pfz_f += $neighbour_set[$i][1] * $neighbour_set[$i][2];
$pfm_f += $neighbour_set[$i][1];
}
$p_arr[0][0] = 6;
$p_arr[0][1] = $pfz_f/sqrt($pfm_f);
if($p_arr[0][1]>3){
echo "推荐f";
}
 
//计算Leo对g的评分
$pfz_g = 0;
$pfm_g = 0;
for($i=0;$i<3;$i++){
$pfz_g += $neighbour_set[$i][1] * $neighbour_set[$i][3];
$pfm_g += $neighbour_set[$i][1];
$p_arr[1][0] = 7;
$p_arr[1][1] = $pfz_g/sqrt($pfm_g);
}
if($p_arr[0][1]>3){
echo "推荐g";
}
 
//计算Leo对h的评分
$pfz_h = 0;
$pfm_h = 0;
for($i=0;$i<3;$i++){
$pfz_h += $neighbour_set[$i][1] * $neighbour_set[$i][4];
$pfm_h += $neighbour_set[$i][1];
$p_arr[2][0] = 8;
$p_arr[2][1] = $pfz_h/sqrt($pfm_h);
}
print_r($p_arr);
if($p_arr[0][1]>3){
echo "推荐h";
}
$p_arr是对Leo的推荐数组,其内容类似如下;
Array ( [0] => Array ( [0] => 6 [1] => 4.2314002228795 ) [1] => Array ( [0] => 7 [1] => 2.6511380196197 ) [2] => Array ( [0] => 8 [1] => 0.45287424581774 ) )

f is the 6th column, the Predict value is 4.23, g is the seventh column, the Predict value is 2.65...

Finished f, g, h There are two processing methods after the Predict value: one is to recommend items with a Predict value greater than 3 to Leo, and the other is to sort the Predict values ​​from large to small and recommend the top 2 items with large Predict values ​​to Leo. This code was not written.

As can be seen from the above example, the implementation of the recommendation algorithm is very troublesome, requiring looping, judgment, merging arrays, etc. If not handled properly, it will become a burden on the system. There are still the following problems in actual processing:

1. In the above example, we only recommend Leo, and we already know that Leo has not evaluated items f, g, h. If put into an actual system, for each user who needs to make a recommendation, it is necessary to find out which items he has not rated, which is another part of the overhead.

2. The entire table query should not be performed. Some standard values ​​can be set in the actual system. For example: We find the Cos value between Leo and other people in the table. If the value is greater than 0.80, it means that they can be neighbors. In this way, when I find 10 neighbors, I stop calculating the Cos value to avoid querying the entire table. This method can also be used appropriately for recommended items. For example, I only recommend 10 items, and stop calculating the Predict value after recommending them.

3. As the system is used, the items will also change. Today it is fgh, and tomorrow it may be xyz. When the items change, the data table needs to be dynamically changed.

4. Content-based recommendations can be appropriately introduced to improve the recommendation algorithm.

5. Recommended accuracy issues. Setting different standard values ​​will affect the accuracy.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Can PHP not use the recommendation algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Can php write front-end?Next article:Can php write front-end?