推荐系统验证(Python+Redis)

首先

从亚马逊的购物中得到的宝贵推荐功能(只有针对自己?),“购买了这个商品的人还购买了这些商品”可能会有一些意外的发现。

关于推荐系统的实施,前辈们已经从理论到实践留下了很多宝贵的知识,非常感谢。

我想要快速尝试一下推荐选项。

我們將驗證在 PyPI 上公開的推薦引擎 cf_recommender。

PyPI(Python Package Index)とは

プログラミング言語Pythonの、サードパーティーソフトウェアリポジトリ
すべてのオープンソースなPythonパッケージの包括的なカタログ

cf_recommenderとは

協調フィルタリング型のリアルタイムレコメンデーションエンジン
Python言語で実装されている
Redis(インメモリデータベース)をバックエンドとして使用

验证环境

硬件

CPU: Intel(R) Core(TM) 3.30GHz(4コア8スレッド)
メモリ: 16 GB

操作系统

我使用了Ubuntu 20.04 LTS。

我使用Windows 10以管理员权限安装了Ubuntu。

> wsl --install -d Ubuntu-20.04

引入Redis

我們使用了 Redis 3.0.7。
由於 cf_recommender 最後的版本是在2016年發佈的,所以我們使用了舊版本。

    • ダウンロード

 

    • http://download.redis.io/releases/

インストール手順
https://www.digitalocean.com/community/tutorials/how-to-install-redis-from-source-on-ubuntu-18-04

make
sudo make install

起動

$ /usr/local/bin/redis-server

或者注册并启动服务。

安装Python及其相关库

使用Python 2.7.18版本。

嗯,能听到声音,但cf_recommender不支持Python 3.5以上版本,使用了大约2016年左右的旧版Python和相关库。

$ sudo apt update
$ sudo apt install python2
$ python --version
Python 2.7.18

使用pip版本20.3.4

$ curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
$ python get-pip.py
$ pip --version
pip 20.3.4 from /home/users/.local/lib/python2.7/site-packages/pip (python 2.7)

使用redis-py 2.10.5。

这边也在使用旧版本。

$ pip install -Iv redis==2.10.5

引入cf_recommender

$ pip install cf_recommender

确认

$ pip list
Package        Version
-------------- -------
cf-recommender 1.0.1
pip            20.3.4
redis          2.10.5

使用示例代码进行推荐验证

我在以下的代码中尝试了一下。

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from cf_recommender.recommender import Recommender

cf_settings = {
    # redis
    'expire': 3600 * 24 * 30,
    'redis': {
        'host': 'localhost',
        'port': 6379,
        'db': 0
    },
    # recommendation engine settings
    'recommendation_count': 10,
    'recommendation': {
        'update_interval_sec': 600,
        'search_depth': 100,
        'max_history': 1000,
    },
}

recommendation = Recommender(cf_settings)

# register history
user_id = 'user-00001'
buy_items = ['Item10', 'Item10', 'Item10', 'Item3', 'Item3', 'Item1']
for item_id in buy_items:
    recommendation.register(item_id)
recommendation.like(user_id, buy_items)

user_id = 'user-00002'
buy_items = ['Item10', 'Item9', 'Item9', 'Item3', 'Item2', 'Item1']
for item_id in buy_items:
    recommendation.register(item_id)
recommendation.like(user_id, buy_items)

user_id = 'user-00003'
buy_items = ['Item10', 'Item10', 'Item4', 'Item3', 'Item1', 'Item1']
for item_id in buy_items:
    recommendation.register(item_id)
recommendation.like(user_id, buy_items)

item_id = 'Item1'
print '商品 Item1 を見た人は以下の商品も見ています'
print recommendation.get(item_id, count=2)

一旦执行

$ python sample.py
商品 Item1 を見た人は以下の商品も見ています
['Item10', 'Item3']

最后

非常简单,但我尝试验证了推荐引擎cf_commendation。使用这个引擎可以实现各种有趣的功能。

广告
将在 10 秒后关闭
bannerAds