A
A
alexesDev2016-05-23 11:45:12
Algorithms
alexesDev, 2016-05-23 11:45:12

Is there somewhere a description of the algorithm for finding a group of products that are bought together?

You need to find groups of 3-4 products that are most often bought together. Is this problem solved only by exhaustive search or is there something tricky?
That is, there is a list of orders and positions. I would like to make a prediction of what they forgot to add to the basket.
While stupidly doing a three-dimensional matrix and in the cell I write down the number of matches for orders.

with product_ids as (
  select distinct product_id as id
    from order_items
   where created_at > current_date - interval '1' day
), product_orders as (
  select array_agg(product_id) as product_ids
    from order_items
   where created_at > current_date - interval '1' day
   group by order_id
)
select *
  from (
    select p1.id as id1, p2.id as id2, p3.id as id3
         , (select count(*) from product_orders
             where p1.id = any(product_ids) 
                 and p2.id = any(product_ids)
                 and p3.id = any(product_ids)) as count
      from product_ids p1, product_ids p2, product_ids p3
  ) t
 where count > 1
 order by count;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evgeniy_lm, 2016-05-23
@evgeniy_lm

Why bust?
You select all orders with a certain product, sum up the number of other products, select the desired number of the most purchased.
PS I hope you don't need to explain what SQL is

X
xmoonlight, 2016-05-23
@xmoonlight

Collaborative_filtering.gifFrom wikipedia :
That's bullshit!!!!! But what about the bundle: selection extremes (the most expensive / cheapest, maximum / minimum price on the day of sale (from products with similar characteristics), the most optimal price / performance ratio, etc.) + neural network? ))))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question