推荐算法是会经常遇到的技术。主要解决的是问题是:如果你喜欢书 A,那么你可能会喜欢书 B。
本文我们使用 MySQL ,基于数据统计,拆解实现了一个简单的推荐算法。
首先,创建一个 用户喜欢的书数据表,所表示的是 user\_id 喜欢 book\_id。
CREATE TABLE user_likes (
user_id INT NOT NULL,
book_id VARCHAR(10) NOT NULL,
PRIMARY KEY (user_id,book_id),
UNIQUE KEY book_id (book_id, user_id)
);
CREATE TABLE user_likes_similar (
user_id INT NOT NULL,
liked_user_id INT NOT NULL,
rank INT NOT NULL,
KEY book_id (user_id, liked_user_id)
);
插入4条测试数据
INSERT INTO user_likes VALUES (1, 'A'), (1, 'B'), (1, 'C');
INSERT INTO user_likes VALUES (2, 'A'), (2, 'B'), (2, 'C'), (2,'D');
INSERT INTO user_likes VALUES (3, 'X'), (3, 'Y'), (3, 'C'), (3,'Z');
INSERT INTO user_likes VALUES (4, 'W'), (4, 'Q'), (4, 'C'), (4,'Z');
代表的含义为:用户 1 喜欢A、B、C,用户 2 喜欢 A、B、C、D,用户 3 喜欢 X、Y、C、Z,用户 4 喜欢 W、Q、C、Z。
以为用户 1 计算推荐书籍为例,我们需要计算用户 1 和其他用户的相似度,然后根据相似度排序。
清空相似度数据表
DELETE FROM user_likes_similar WHERE user_id = 1;
计算用户相似度数据表
INSERT INTO user_likes_similar
SELECT 1 AS user_id, similar.user_id AS liked_user_id, COUNT(*) AS rank
FROM user_likes target
JOIN user_likes similar ON target.book_id= similar.book_id AND target.user_id != similar.user_id
WHERE target.user_id = 1
GROUP BY similar.user_id ;
可以看到查找到的相似度结果为
user_id, liked_user_id, rank
1, 2, 2
1, 3, 1
1, 4, 1
然后根据相似度排序,取前 10 个,就是推荐的书籍了。
SELECT similar.book_id, SUM(user_likes_similar.rank) AS total_rank
FROM user_likes_similar
JOIN user_likes similar ON user_likes_similar.liked_user_id = similar.user_id
LEFT JOIN user_likes target ON target.user_id = 1 AND target.book_id = similar.book_id
WHERE user_likes_similar.user_id = 1 AND target.book_id IS NULL
GROUP BY similar.book_id
ORDER BY total_rank desc
LIMIT 10;