PageRenderTime 39ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/manual/source/templates/recommendation/quickstart.html.md.erb

https://gitlab.com/github-cloud-corporation/incubator-predictionio
Ruby HTML | 357 lines | 284 code | 73 blank | 0 comment | 3 complexity | 1d2040df5fd7cc318f2d98184870edb7 MD5 | raw file
  1. ---
  2. title: Quick Start - Recommendation Engine Template
  3. ---
  4. ## Overview
  5. This Recommendation Engine Template has integrated **Apache Spark MLlib**'s
  6. Collaborative Filtering algorithm by default. You can customize it easily to fit
  7. your specific needs.
  8. We are going to show you how to create your own recommendation engine for
  9. production use based on this template.
  10. ## Usage
  11. ### Event Data Requirements
  12. By default, the template requires the following events to be collected:
  13. - user 'rate' item events
  14. - user 'buy' item events
  15. NOTE: You can customize to use other event.
  16. ### Input Query
  17. - user ID
  18. - num of recomended items
  19. ### Output PredictedResult
  20. - a ranked list of recommended itemIDs
  21. ## 1. Install and Run PredictionIO
  22. <%= partial 'shared/quickstart/install' %>
  23. ## 2. Create a new Engine from an Engine Template
  24. <%= partial 'shared/quickstart/create_engine', locals: { engine_name: 'MyRecommendation', template_name: 'Recommendation Engine Template', template_repo: 'template-scala-parallel-recommendation' } %>
  25. ## 3. Generate an App ID and Access Key
  26. <%= partial 'shared/quickstart/create_app' %>
  27. ## 4. Collecting Data
  28. Next, let's collect some training data. By default,
  29. the Recommendation Engine Template supports 2 types of events: **rate** and
  30. **buy**. A user can give a rating score to an item or he can buy an item. This template requires user-view-item and user-buy-item events.
  31. INFO: This template can easily be customized to consider more user events such as *like*, *dislike* etc.
  32. <%= partial 'shared/quickstart/collect_data' %>
  33. For example, when a user with ID "u0" rate an item with ID "i0" with rating of 5 on time `2014-11-02T09:39:45.618-08:00` (current time will be used if eventTime is not specified), you can send the following "rate" event. Run the following `curl` command:
  34. <div class="tabs">
  35. <div data-tab="REST API" data-lang="json">
  36. ```
  37. $ curl -i -X POST http://localhost:7070/events.json?accessKey=$ACCESS_KEY \
  38. -H "Content-Type: application/json" \
  39. -d '{
  40. "event" : "rate",
  41. "entityType" : "user",
  42. "entityId" : "u0",
  43. "targetEntityType" : "item",
  44. "targetEntityId" : "i0",
  45. "properties" : {
  46. "rating" : 5
  47. }
  48. "eventTime" : "2014-11-02T09:39:45.618-08:00"
  49. }'
  50. ```
  51. </div>
  52. <div data-tab="Python SDK" data-lang="python">
  53. ```python
  54. import predictionio
  55. client = predictionio.EventClient(
  56. access_key=<ACCESS KEY>,
  57. url=<URL OF EVENTSERVER>,
  58. threads=5,
  59. qsize=500
  60. )
  61. # A user rates an item
  62. client.create_event(
  63. event="rate",
  64. entity_type="user",
  65. entity_id=<USER ID>,
  66. target_entity_type="item",
  67. target_entity_id=<ITEM ID>,
  68. properties= { "rating" : float(<RATING>) }
  69. )
  70. ```
  71. </div>
  72. <div data-tab="PHP SDK" data-lang="php">
  73. ```php
  74. <?php
  75. require_once("vendor/autoload.php");
  76. use predictionio\EventClient;
  77. $client = new EventClient(<ACCESS KEY>, <URL OF EVENTSERVER>);
  78. // A user rates an item
  79. $client->createEvent(array(
  80. 'event' => 'rate',
  81. 'entityType' => 'user',
  82. 'entityId' => <USER ID>,
  83. 'targetEntityType' => 'item',
  84. 'targetEntityId' => <ITEM ID>,
  85. 'properties' => array('rating'=> <RATING>)
  86. ));
  87. ?>
  88. ```
  89. </div>
  90. <div data-tab="Ruby SDK" data-lang="ruby">
  91. ```ruby
  92. # Create a client object.
  93. client = PredictionIO::EventClient.new(<ACCESS KEY>, <URL OF EVENTSERVER>)
  94. # A user rates an item.
  95. client.create_event(
  96. 'rate',
  97. 'user',
  98. <USER ID>, {
  99. 'targetEntityType' => 'item',
  100. 'targetEntityId' => <ITEM ID>,
  101. 'properties' => { 'rating' => <RATING (float)> }
  102. }
  103. )
  104. ```
  105. </div>
  106. <div data-tab="Java SDK" data-lang="java">
  107. ```java
  108. import org.apache.predictionio.Event;
  109. import org.apache.predictionio.EventClient;
  110. EventClient client = new EventClient(<ACCESS KEY>, <URL OF EVENTSERVER>);
  111. // A user rates an item
  112. Event rateEvent = new Event()
  113. .event("rate")
  114. .entityType("user")
  115. .entityId(<USER_ID>)
  116. .targetEntityType("item")
  117. .targetEntityId(<ITEM_ID>)
  118. .property("rating", new Float(<RATING>));
  119. client.createEvent(rateEvent);
  120. ```
  121. </div>
  122. </div>
  123. When a user with ID "u1" buy an item with ID "i2" on time `2014-11-10T12:34:56.123-08:00` (current time will be used if eventTime is not specified), you can send the following "rate" event. Run the following `curl` command:
  124. <div class="tabs">
  125. <div data-tab="REST API" data-lang="json">
  126. ```
  127. $ curl -i -X POST http://localhost:7070/events.json?accessKey=$ACCESS_KEY \
  128. -H "Content-Type: application/json" \
  129. -d '{
  130. "event" : "buy",
  131. "entityType" : "user",
  132. "entityId" : "u1",
  133. "targetEntityType" : "item",
  134. "targetEntityId" : "i2",
  135. "eventTime" : "2014-11-10T12:34:56.123-08:00"
  136. }'
  137. ```
  138. </div>
  139. <div data-tab="Python SDK" data-lang="python">
  140. ```python
  141. # A user buys an item
  142. client.create_event(
  143. event="buy",
  144. entity_type="user",
  145. entity_id=<USER ID>,
  146. target_entity_type="item",
  147. target_entity_id=<ITEM ID>
  148. )
  149. ```
  150. </div>
  151. <div data-tab="PHP SDK" data-lang="php">
  152. ```php
  153. <?php
  154. // A user buys an item
  155. $client->createEvent(array(
  156. 'event' => 'buy',
  157. 'entityType' => 'user',
  158. 'entityId' => <USER ID>,
  159. 'targetEntityType' => 'item',
  160. 'targetEntityId' => <ITEM ID>
  161. ));
  162. ?>
  163. ```
  164. </div>
  165. <div data-tab="Ruby SDK" data-lang="ruby">
  166. ```ruby
  167. # A user buys an item.
  168. client.create_event(
  169. 'buy',
  170. 'user',
  171. <USER ID>, {
  172. 'targetEntityType' => 'item',
  173. 'targetEntityId' => <ITEM ID>
  174. }
  175. )
  176. ```
  177. </div>
  178. <div data-tab="Java SDK" data-lang="java">
  179. ```java
  180. // A user buys an item
  181. Event buyEvent = new Event()
  182. .event("buy")
  183. .entityType("user")
  184. .entityId(<USER_ID>)
  185. .targetEntityType("item")
  186. .targetEntityId(<ITEM_ID>);
  187. client.createEvent(buyEvent);
  188. ```
  189. </div>
  190. </div>
  191. <%= partial 'shared/quickstart/query_eventserver' %>
  192. ### Import More Sample Data
  193. <%= partial 'shared/quickstart/import_sample_data' %>
  194. A Python import script `import_eventserver.py` is provided in the template to import the data to
  195. Event Server using Python SDK. Please upgrade to the latest Python SDK.
  196. <%= partial 'shared/quickstart/install_python_sdk' %>
  197. Replace the value of access_key parameter by your
  198. **Access Key** and run:
  199. WARNING: These commands must be executed in the Engine directory, for example: `MyRecomendation`.
  200. ```
  201. $ cd MyRecommendation
  202. $ curl https://raw.githubusercontent.com/apache/spark/master/data/mllib/sample_movielens_data.txt --create-dirs -o data/sample_movielens_data.txt
  203. $ python data/import_eventserver.py --access_key $ACCESS_KEY
  204. ```
  205. You should see the following output:
  206. ```
  207. Importing data...
  208. 1501 events are imported.
  209. ```
  210. Now the movie ratings data is stored as events inside the Event Store.
  211. <%= partial 'shared/quickstart/query_eventserver_short' %>
  212. INFO: By default, the template train the model with "rate" events (explicit rating). You can customize the engine to [read other custom events](/templates/recommendation/reading-custom-events/) and [handle events of implicit preference (such as, view, buy)](/templates/recommendation/training-with-implicit-preference/)
  213. ## 5. Deploy the Engine as a Service
  214. <%= partial 'shared/quickstart/deploy_enginejson', locals: { engine_name: 'MyRecommendation' } %>
  215. <%= partial 'shared/quickstart/deploy', locals: { engine_name: 'MyRecommendation' } %>
  216. ## 6. Use the Engine
  217. Now, You can try to retrieve predicted results. To recommend 4 movies to user
  218. whose id is 1, you send this JSON `{ "user": "1", "num": 4 }` to the deployed
  219. engine and it will return a JSON of the recommended movies. Simply send a query
  220. by making a HTTP request or through the `EngineClient` of an SDK.
  221. With the deployed engine running, open another temrinal and run the following `curl` command or use SDK to send the query:
  222. <div class="tabs">
  223. <div data-tab="REST API" data-lang="json">
  224. ```
  225. $ curl -H "Content-Type: application/json" \
  226. -d '{ "user": "1", "num": 4 }' http://localhost:8000/queries.json
  227. ```
  228. </div>
  229. <div data-tab="Python SDK" data-lang="python">
  230. ```python
  231. import predictionio
  232. engine_client = predictionio.EngineClient(url="http://localhost:8000")
  233. print engine_client.send_query({"user": "1", "num": 4})
  234. ```
  235. </div>
  236. <div data-tab="PHP SDK" data-lang="php">
  237. ```php
  238. <?php
  239. require_once("vendor/autoload.php");
  240. use predictionio\EngineClient;
  241. $client = new EngineClient('http://localhost:8000');
  242. $response = $client->sendQuery(array('user'=> 1, 'num'=> 4));
  243. print_r($response);
  244. ?>
  245. ```
  246. </div>
  247. <div data-tab="Ruby SDK" data-lang="ruby">
  248. ```ruby
  249. # Create client object.
  250. client = PredictionIO::EngineClient.new(<ENGINE DEPLOY URL>)
  251. # Query PredictionIO.
  252. response = client.send_query('user' => <USER ID>, 'num' => <NUMBER (integer)>)
  253. puts response
  254. ```
  255. </div>
  256. <div data-tab="Java SDK" data-lang="java">
  257. ```java
  258. import com.google.common.collect.ImmutableMap;
  259. import com.google.gson.JsonObject;
  260. import org.apache.predictionio.EngineClient;
  261. // create client object
  262. EngineClient engineClient = new EngineClient(<ENGINE DEPLOY URL>);
  263. // query
  264. JsonObject response = engineClient.sendQuery(ImmutableMap.<String, Object>of(
  265. "user", "1",
  266. "num", 4
  267. ));
  268. ```
  269. </div>
  270. </div>
  271. The following is sample JSON response:
  272. ```
  273. {
  274. "itemScores":[
  275. {"item":"22","score":4.072304374729956},
  276. {"item":"62","score":4.058482414005789},
  277. {"item":"75","score":4.046063009943821},
  278. {"item":"68","score":3.8153661512945325}
  279. ]
  280. }
  281. ```
  282. Congratulations, *MyRecommendation* is now running!
  283. <%= partial 'shared/quickstart/production' %>
  284. #### [Next: DASE Components Explained](/templates/recommendation/dase/)