“People who watched this also watched” algorithm

后端 未结 5 862
攒了一身酷
攒了一身酷 2021-02-14 03:45

I am trying to code an algorithm that acts a bit like Amazon\'s \"People who bought this also bought\".

The difference between the two is that mine is just counting the

5条回答
  •  情书的邮戳
    2021-02-14 04:16

    I was able to achieve your desired result using a simple DB structure, and a pretty simple query:

    Table

    TABLE `exa`
    
    | sesh_id | prod_id |
    ---------------------
    | 1       | 1       |
    | 1       | 2       |
    | 1       | 3       |
    | 1       | 4       |
    | 2       | 2       |
    | 2       | 3       |
    | 2       | 4       |
    | 3       | 3       |
    | 3       | 4       |
    | 4       | 1       |
    | 4       | 2       |
    | 4       | 5       |
    

    Query

    SELECT c.prod_id, COUNT(*)
    FROM `exa` a
    JOIN `exa` b ON a.prod_id=b.prod_id
    JOIN `exa` c ON b.sesh_id=c.sesh_id
    WHERE a.`prod_id`=3 AND c.prod_id!=3
    GROUP BY c.prod_id
    ORDER BY 2 DESC;
    

    Result

    | prod_id | COUNT |
    | 4       | 9     |
    | 2       | 6     |
    | 1       | 3     |
    

    The idea is that every time a session views a product, it gets inserted into the table [in this case exa]

    Then, on any particular product view, you can check and see what other products people who viewed this product also viewed, weighted by frequency. So in this particular example, EVERYONE that viewed product #3 viewed product #4, so it comes up first in the sort. Product #5 was only viewed by session #4, and session #4 didn't view product #3, so product #5 doesn't come up in the results.

提交回复
热议问题