Home Backend Development PHP Problem Can PHP not use the recommendation algorithm?

Can PHP not use the recommendation algorithm?

Nov 02, 2019 am 09:28 AM
php Recommendation algorithm

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);
Copy after login

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[][]是一个二维数组
}
Copy after login

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/>";
}
Copy after login

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);
Copy after login

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/>";
Copy after login

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的推荐数组,其内容类似如下;
Copy after login
Array ( [0] => Array ( [0] => 6 [1] => 4.2314002228795 ) [1] => Array ( [0] => 7 [1] => 2.6511380196197 ) [2] => Array ( [0] => 8 [1] => 0.45287424581774 ) )
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles