#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):
2. Prediction formula (predict a What kind of items may you like):
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);
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:
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:
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:
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.