a recommendation engine for your php application

101
A recommendation engine for your PHP apps

Upload: michele-orselli

Post on 13-Apr-2017

181 views

Category:

Software


4 download

TRANSCRIPT

Page 1: A recommendation engine for your php application

A recommendation engine for your PHP apps

Page 2: A recommendation engine for your php application

1) Intro to recommender systems

2) PredictionIO

3) Case Study

Page 3: A recommendation engine for your php application

Definition: a system that help people finding things when the process of finding what you need is challenging because you have a lot of choices/alternatives

Page 4: A recommendation engine for your php application

So… it’s a search engine!

Page 5: A recommendation engine for your php application

Search Engines

Document base is (almost) static

Queries are dynamic

Page 6: A recommendation engine for your php application

Search Engines

Create an index analyzing the documents

Calculate relevance for a query: tf*idf

Page 7: A recommendation engine for your php application

Recommender systems

Document base is growing (eg: Netflix)

Query is static: find something I like

Page 8: A recommendation engine for your php application

Classification

Page 9: A recommendation engine for your php application

Domain: news, products, …

Helps defining what can be suggested

Page 10: A recommendation engine for your php application

Purpose: sales, information, education, build a community

What is TripAdvisor purpose?

Page 11: A recommendation engine for your php application

Personalization levels

• Non personalized: best sellers

• Demographic: age, location

• Ephemeral: based on current activities

• Persistent

Page 12: A recommendation engine for your php application

Types of input

• Explicit: ask user to rate something

• Implicit: inferred from user behaviour

Page 13: A recommendation engine for your php application

Output

• Prediction: predicted rating, evaluation

• Recommendations: suggestion list, top-n, offers, promotion

• Filtering: email filters, news articles

Page 14: A recommendation engine for your php application

A model for comparison

Page 15: A recommendation engine for your php application

User: people with preference

Items: subject of rating

Rating: expression of opinion

(Community: space where opinions makes sense)

Page 16: A recommendation engine for your php application
Page 17: A recommendation engine for your php application

Non-personalized

Page 18: A recommendation engine for your php application

Best seller

Most popular

Trending

Summary of community ratings: eg best hotel in town

Page 19: A recommendation engine for your php application
Page 20: A recommendation engine for your php application
Page 21: A recommendation engine for your php application

Hotel

Page 22: A recommendation engine for your php application

Visitor Hotel

Page 23: A recommendation engine for your php application

Visitor Hotel

Page 24: A recommendation engine for your php application

Hotel A Hotel B Hotel C

John 3 5

Jane 3

Fred 1 0

Tom 4

AVG 3.5 3 0

Page 25: A recommendation engine for your php application

Content based

Page 26: A recommendation engine for your php application

User rate items

We build a model of user preference

Look for similar items based on the model

Page 27: A recommendation engine for your php application
Page 28: A recommendation engine for your php application

Action 0.7

Sci Fi 3.2

Vin Diesel 1.2

… …

https://www.amazon.com/Relevant-Search-applications-Solr-Elasticsearch/dp/161729277Xhttp://www.slideshare.net/treygrainger/building-a-real-time-solrpowered-recommendation-engine

Page 29: A recommendation engine for your php application

Problems/Limitations

Page 30: A recommendation engine for your php application

Need to know items content

User cold start: time to learn important features for the user

What if user interest change?

Lack of serendipity: accidentally discover something you like

Page 31: A recommendation engine for your php application

Collaborative filtering

Page 32: A recommendation engine for your php application

No need to analyze (index) content

Can capture more subtle things

Serendipity

Page 33: A recommendation engine for your php application

User-User

Select people of my neighborhood with similar taste. If other people share my taste I want their opinion combined

Page 34: A recommendation engine for your php application

E.T

2 4Joe 2 2 3 ?

1 55 2 4 …

Tom 3 3 2

4 1

User-User: which users have similar tastes?

Page 35: A recommendation engine for your php application

E.T

2 4Joe 2 2 3 ?

1 55 2 4 …

Tom 3 3 2

4 1

User-User: which users have similar tastes?

Page 36: A recommendation engine for your php application

Item-Item

Find an items where I have expressed an opinion and look how other people felt about it. Precompute similarities between items

Page 37: A recommendation engine for your php application

E.T

2 4Joe 2 2 3 ?

1 55 2 4 …

Tom 3 3

4 1

Item-Item: which item are similar?

Page 38: A recommendation engine for your php application

Problems/Limitations

Page 39: A recommendation engine for your php application

Sparsity

When recommending from a large item set, users will have rated only some of the items

Page 40: A recommendation engine for your php application

User Cold start

Not enough known about new user to decide who is similar

Page 41: A recommendation engine for your php application

Item cold start

Cannot predict ratings for new item till some similar users have rated it [No problem for content-based]

Page 42: A recommendation engine for your php application

Scalability

With millions of ratings, computations become slow

Page 43: A recommendation engine for your php application

Dimensionality reduction

Page 44: A recommendation engine for your php application

Express my opinions as a set of tastes

Compact representation of the matrix with relevant features

Page 45: A recommendation engine for your php application

Rogue One

1 3 5

Joe 1 2 3

Page 46: A recommendation engine for your php application

An example

Page 47: A recommendation engine for your php application

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

How similar are Joe and Tom? How similar are Joe and Bob?

Page 48: A recommendation engine for your php application

Only consider items both users have rated

For each item - Calculate difference in the users’ ratings - Take the average of this difference over the items

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

Page 49: A recommendation engine for your php application

Sim(Joe, Tom) = (|8-2| + |2-7| + |7-5|)/3 = 13/3 = 4.3 Sim(Alice, Bob) = (|5-7| + |4-1| + |4-3| + |7-8|)/4 = 7/4 = 1.75

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

Page 50: A recommendation engine for your php application

Now we have a score or weight for each user

Page 51: A recommendation engine for your php application

Recommend what similar user have rated highly

To calculate rating of an item to recommend, give weight to each user’s recommendations based on how similar they are to you.

Page 52: A recommendation engine for your php application

use entire matrix or

use a K-nn algorithm: people who historically have the same tastes as me

aggregate using weighted sum

weights depends on similarity

Page 53: A recommendation engine for your php application

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

How similar are Item1 and Item2? How similar are Item1 and Item3?

Page 54: A recommendation engine for your php application

Only consider items both users have rated

For each item - Calculate difference in ratings for the 2 items - Take the average of this difference over the users

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

Page 55: A recommendation engine for your php application

Sim(I1, I2) = (|8-1| + |5-4| + |7-1|)/3 = 14/3 = 4,6

Sim(I1, I3) = (|2-5| + |5-7| + |7-7|)/3 = 5/3 = 1,6

Item1 Item2 Item3 Item4 Item5

Joe 8 1 ? 2 7

Tom 2 ? 5 7 5

Alice 5 4 7 4 7

Bob 7 1 7 3 8

Page 56: A recommendation engine for your php application

As user-user, use whole matrix or identify neighbors

Page 57: A recommendation engine for your php application

Cosine similarity

[3,5]

[2,7]

[0,0]

Page 58: A recommendation engine for your php application

Our domain

Page 59: A recommendation engine for your php application

Domain: online book shop, both paper and digital

Recommend titles, old and news

- Who bought this also bought

- You might like

Page 60: A recommendation engine for your php application

Choosing the tool

Page 61: A recommendation engine for your php application

PredictionIO

Page 62: A recommendation engine for your php application

Under the Apache umbrella

Based on solid open source stack

Customizable templates engines

SDK for PHP

Page 63: A recommendation engine for your php application
Page 64: A recommendation engine for your php application

Installation

http://actionml.com/docs/pio_by_actionml

Pre-baked Amazon AMIs

Page 65: A recommendation engine for your php application

Installation via source code

http://predictionio.incubator.apache.org/install/install-sourcecode/

Page 66: A recommendation engine for your php application

You can choose storage

mysql/postgres vs elasticsearch+hbase

Page 67: A recommendation engine for your php application

The event server

Page 68: A recommendation engine for your php application
Page 69: A recommendation engine for your php application

Pattern: user -- action -- item

User 1 purchased product X

User 2 viewed product Y

User 1 added product Z in the cart

Page 70: A recommendation engine for your php application

$ pio app new MyApp1

[INFO] [App$] Initialized Event Store for this app ID: 1. [INFO] [App$] Created new app: [INFO] [App$] Name: MyApp1 [INFO] [App$] ID: 1 [INFO] [App$] Access Key: 3mZWDzci2D5YsqAnqNnXH9SB6Rg3dsTBs8iHkK6X2i54IQsIZI1eEeQQyMfs7b3F

$ pio eventserver

Page 71: A recommendation engine for your php application

Server runs on port 7070 by default

$ curl -i -X GET http://localhost:7070

{“status":"alive"}

Page 72: A recommendation engine for your php application

$ curl -i -X GET “http://localhost:7070/events.json?accessKey=$ACCESS_KEY"

Page 73: A recommendation engine for your php application

Events modeling

what can/should we model?

rate, like, buy, view, depending on the algorithm

Page 74: A recommendation engine for your php application
Page 75: A recommendation engine for your php application
Page 76: A recommendation engine for your php application
Page 77: A recommendation engine for your php application

$set , $unset and $delete

_pio* are reserved

Page 78: A recommendation engine for your php application

setUser($uid, array $properties=array(), $eventTime=null)

unsetUser($uid, array $properties, $eventTime=null)

deleteUser($uid, $eventTime=null)

setItem($iid, array $properties=array(), $eventTime=null)

unsetItem($iid, array $properties, $eventTime=null)

deleteItem($iid, $eventTime=null)

recordUserActionOnItem($event, $uid, $iid, array $properties=array(), $eventTime=null)

createEvent(array $data)

getEvent($eventId)

Page 79: A recommendation engine for your php application

Engines

Page 80: A recommendation engine for your php application
Page 81: A recommendation engine for your php application

D.A.S.E Architecture

Data Source and Preparation

Algorithm

Serving

Evaluation

Page 82: A recommendation engine for your php application

$ pio template get apache/incubator-predictionio-template-recommender MyRecommendation

$ cd MyRecommendation

Page 83: A recommendation engine for your php application

engine.json

"datasource": { "params" : { "appName": “MyApp1”, "eventNames": [“buy”, “view”] } },

Page 84: A recommendation engine for your php application

$ pio build —verbose

$ pio train

$ pio deploy

Page 85: A recommendation engine for your php application

Getting recommendations

Page 86: A recommendation engine for your php application
Page 87: A recommendation engine for your php application
Page 88: A recommendation engine for your php application
Page 89: A recommendation engine for your php application

Implementation

Page 90: A recommendation engine for your php application
Page 91: A recommendation engine for your php application

2 kind of suggestions

- who bought this also bought (recommendation)

- you may like (similarities)

Page 92: A recommendation engine for your php application

View

Like (add to basket, add to wishlist)

Conversion (buy)

Recorded in batch

Page 93: A recommendation engine for your php application

4 engines

2 for books, 2 for ebooks

(not needed now)

Retrained every night with new data

Page 94: A recommendation engine for your php application

recordLike($user, array $item)

recordConversion($user, array $item)

recordView($user, array $item)

createUser($uid)

Page 95: A recommendation engine for your php application

getRecommendation($uid, $itype, $n = self::N_SUGGESTION)

getSimilarity($iid, $itype, $n = self::N_SUGGESTION)

Page 96: A recommendation engine for your php application
Page 97: A recommendation engine for your php application

user cold start/item cold start

if we don’t get enough suggestion switch to non personalized (also for non logged users)

Page 98: A recommendation engine for your php application
Page 99: A recommendation engine for your php application

user cold start/item cold start

if we don’t get enough suggestion switch to non personalized (best sellers)

Page 100: A recommendation engine for your php application

Michele Orselli CTO@Ideato

_orso_

micheleorselli / ideatosrl

[email protected]

https://joind.in/talk/93d2d

Page 101: A recommendation engine for your php application

Links• http://www.slideshare.net/NYCPredictiveAnalytics/building-a-recommendation-

engine-an-example-of-a-product-recommendation-engine?next_slideshow=1

• https://www.coursera.org/learn/recommender-systems-introduction

• http://actionml.com/

• https://github.com/grahamjenson/ger