PredictionIO:开源的推荐系统

PredictionIO

PredictionIO 是一个用Scala编写的开源机器学习服务器应用,可以帮助你方便地使用RESTFul API搭建推荐引擎。 PredictionIO的核心使用的是一个可伸缩的机器学习库,基于Spark一个完整的端到端Pipeline,让使用者可以非常简单的从零开始搭建一个推荐系统。 "

PredictionIO 是由三个元件所组成:

  • PredictionIO platform
  • Event Server: 收集来自应用程式的资料,可以是即时也可以定时。
  • Engine: 训练模型,并且将结果以 Restful API 提供查询。

PredictionIO

Install

官方有提供快速的一键安装方法,当然也可以手动安装

$ bash -c "$(curl -s https://install.prediction.io/install.sh)"
$ PATH=$PATH:/home/yourname/PredictionIO/bin; export PATH
复制代码

透过以下指定可以检查是否安装成功,会回传每一种套件所连接的状况

$ pio status

### Return:
[INFO] [Console$] Inspecting PredictionIO...
[INFO] [Console$] PredictionIO 0.9.6 is installed at ...
[INFO] [Console$] Inspecting Apache Spark...
[INFO] [Console$] Apache Spark is installed at ...
[INFO] [Console$] Apache Spark 1.6.0 detected ...
[INFO] [Console$] Inspecting storage backend connections...
[INFO] [Storage$] Verifying Meta Data Backend (Source: MYSQL)...
[INFO] [Storage$] Verifying Model Data Backend (Source: MYSQL)...
[INFO] [Storage$] Verifying Event Data Backend (Source: MYSQL)...
[INFO] [Storage$] Test writing to Event Store (App Id 0)...
[INFO] [Console$] (sleeping 5 seconds for all messages to show up...)
[INFO] [Console$] Your system is all ready to go.
复制代码

Quick Start

Step 1. Run PredictionIO

先执行 PredictionIO 主程式,针对不同的储存器,有不同的执行方法。

$ pio eventserver &
# If you are using PostgreSQL or MySQL, run the following to start PredictionIO Event Server

or

$ pio-start-all
# If instead you are running HBase and Elasticsearch, run the following to start all PredictionIO Event Server, HBase, and Elasticsearch
复制代码

Step 2. Create a new Engine from an Engine Template

选择 Engine Templates 一个适合的 Engine。

$ pio template get <template-repo-path> <your-app-directory>
$ cd MyRecommendation
复制代码

可以从 Engine Templates 选择,也可以自定义,在这边我们使用 Universal Recommender 作为范例。

Step 3. Generate an App ID and Access Key

执行指定从 Engine 产生一个 APP 并取得对应的 Key。

$ pio app new MyRecommendation

### Return:
[INFO] [App$] Initialized Event Store for this app ID: 1.
[INFO] [App$] Created new app:
[INFO] [App$] Name: MyRecommendation
[INFO] [App$] ID: 1
[INFO] [App$] Access Key: ...

$ pio app list

### Return:
[INFO] [App$] Name | ID | Access Key | Allowed Event(s)
[INFO] [App$] MyRecommendation | 1 | ... | (all)
[INFO] [App$] Finished listing 1 app(s).
复制代码

Step 4. Collecting Data

接着要汇入资料,最基本的推荐演算法(Cooperative Filtering, CF)格式支元: user - action - item 三种元素。使用 data/import_eventserver.py 可以将符合格式的资料汇入资料库。

$ curl <sample_data> --create-dirs -o data/<sample_data>
$ python data/import_eventserver.py --access_key <access-key>
复制代码
...
0::2::3
0::3::1
3::9::4
6::9::1
...
复制代码

Step 5. Deploy the Engine as a Service

在部署应用程式之前,先在 Engine.json 中设定基础资料,像是 appName 或是演算法要运行几次之类的。

  ...
  "datasource": {
    "params" : {
      "appName": MyRecommendation
      # make sure the appName parameter match your App Name
    }
  },
  ...
复制代码

部署系统到 Web Service 时,过程中分成三个步骤: pio build -> pio train -> pio deploy Building 负责准备 Spark 的基础环境及资料准备。 Training 负责执行演算法建模。 Deployment 则是将结果运行在 Web Service 上,并以 Restful API 开放。

  • Bulid and Training the Predictive Model
$ pio build

### Return:
[INFO] [Console$] Your engine is ready for training.


$ pio train

### Return:
[INFO] [CoreWorkflow$] Training completed successfully.

$ pio deploy

### Return:
[INFO] [HttpListener] Bound to /0.0.0.0:8000
[INFO] [MasterActor] Bind successful. Ready to serve.

复制代码

Step 6. Use the Engine

然后就是执行了,预设会开在 port 8000,参数输入 使用者 即要推荐的 商品数量

$ curl -H "Content-Type: application/json" \
-d '{ "user": "1", "num": 4 }' https://localhost:8000/queries.json

### Retnrn:
{
  "itemScores":[
    {"item":"22","score":4.072304374729956},
    {"item":"62","score":4.058482414005789},
    {"item":"75","score":4.046063009943821},
    {"item":"68","score":3.8153661512945325}
  ]
}
复制代码

Reference

  1. PredictionIO
  2. PredictionIO快速入门

License

本著作由Chang Wei-Yaun (v123582)制作, 以创用CC 姓名标示-相同方式分享 3.0 Unported授权条款释出。

猜你喜欢

转载自juejin.im/post/5bc560045188255c2f424733