Collaborative filtering (eng. Collaborative filtering) is a method that gives automatic predictions based on the accumulated information about the interests and tastes of users.
Most often, you can recommend music or books already having some data about the user. For example, Amazon or Ozon remember what I am looking for or where I went and builds a recommendation from me. But it is better that the user himself entered them into his profile, you can even tell him that you have this system and that he himself could choose what to give him next time.
You can
fasten OpenSlopeOne , which is written by a Chinese programmer.
Consider an example. There are 4 users in the bookstore: Vasya, Zhenya, Yura, Larisa and 8 products. About each we have information:
')
Name | Bought (id) | Looked through |
Vasya | 1,6,7 | 2.3 |
Zhenya | 1,2,3 | 5,7,4 |
Yura | 7,8 | 1.4 |
Larisa | eight | 6 |
Algorithm setting:config.ini.php
; <?php exit; ?> DO NOT REMOVE THIS LINE
[database]
host = localhost
username = mysql
password = mysql
dbname =
port = 3306
adapter = PDO_MYSQL ; PDO_MYSQL or MYSQLI
Create two tables:
CREATE TABLE IF NOT EXISTS oso_user_ratings (
user_id int(11) NOT NULL, item_id int(11) NOT NULL, rating decimal(14,4) NOT NULL default '0.0000', KEY item_id (item_id), KEY user_id (user_id,item_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS oso_slope_one (
item_id1 int(11) NOT NULL, item_id2 int(11) NOT NULL, times int(11) NOT NULL, rating decimal(14,4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The oso_user_ratings table is intended for storing user information. If the user bought, then we put 1 in the rating field, if he didn’t buy, then we put 0 (we need to correctly calculate the formula:
similarity of items as cosine between vectors of purchases in the matrix of users and items , but for example I will show the
principle ). And accordingly, in the user_id fields, enter the user id and in item_id enter the product id.
The principle is that in the oso_slope_one table each product generates by weight. For example, if Petya bought this product, and Petya has my goods purchased, then it is logical to think that Petya's products can come to me. The more matches with users, the greater the percentage that I need this product. SlopeOne translates as a slope to one, that is, it primarily displays to me, those products that have the highest priority (bought) from other users with common interests.
test.php
<?php
require './OpenSlopeOne.php';
$openslopeone = new OpenSlopeOne();
$openslopeone->initSlopeOneTable();
$openslopeone->initSlopeOneTable('MySQL');
// id 1
var_dump($openslopeone->getRecommendedItemsByUser(1));
?>
The algorithm recommended to me:
- 8 goods Larissa, t. To. My purchased goods 6 she looked through.
- he recommended 2.3 goods to me, which Zhenya firstly bought with my common product with id 1 and in addition I already looked through it
- etc.
There is a small mistake, he also gives me the goods that the user bought with common interests, but I already have it. This is due to the fact that we recorded data in the table not by the formula, but 0 or 1.
My goal was simply to show the principle of work and give food for thought and why you should push off. Also, each algorithm must be twisted under its public.PS If anyone is familiar with the recommendations supplement in the comments. Maybe even in my article there are errors, tk. Not much test, and the project in which the algorithm was applied is under development.