Suppose we want to predict what customer will buy during his next visit to the Electronic Shop based on his past purchase history. I know that it is a very broad question, but I am new to machine learning and don't have much idea about how to approach this problem.

The simplest thing that comes to my mind is to find the most frequent items that customer has bought and suggest it. However, I don't think that this is a very robust approach as it doesn't consider this scenario:

Computer (1st Purchase) -> Mouse (2nd Purchase) -> Mouse Pad (3rd Purchase)

I am looking for a simple model to get started and scale in terms of features and training data. I would love to hear suggestions of experienced Data Scientist as it is a most common problem.

Thank you.

  • 1
    $\begingroup$ Welcome to the Site! have you come across this techniques name Sequence Mining? if not so that is that technique which you are looking for. You can go through this link for better understanding. All the Best! $\endgroup$
    – Toros91
    Commented Apr 16, 2018 at 9:57

2 Answers 2


Take a look at association rule learning (https://en.wikipedia.org/wiki/Association_rule_learning). A really common algorithm is the Apriori agorithm. You could use the package apyori, it works great: https://pypi.python.org/pypi/apyori/1.1.1

  • $\begingroup$ Thanks for the suggestion, Seems like what I am looking for. But I am little sceptic about its scalability, Does it scale without problem (i.e. Meaning does it works with Large dataset without any problem) $\endgroup$
    – arush1836
    Commented Apr 17, 2018 at 8:25
  • $\begingroup$ In general, you don't need to use this library, the algorithm is pretty simple. I used apyori with just a few million transactions and it worked fine but the run time was not important. You don't need to process whole strings like in the example, you could simple use identifiers like the product id to reduce the memory consuming. For bigger datasets, I recommend to implement a faster (multi-threaded) algorithm, it’s really not complicated. Just take a look at the algorithm description. For testing, apyori should be fine to estimate your prediction quality. $\endgroup$
    – MBDev
    Commented Apr 17, 2018 at 15:45

You can try to measure the similarity of the products that a user has bought so far with other user's purhcases (user based recommendation) or you can try perform associate rule among the items that the user has bought and other items (item based recommendation). You can also perform some clustering techiniques to find group of similar items or users.

Another approach could be the following, if you have available the information for a user's purchase then you can try to predict the user's next purchase. This approach can be a Markov Model. In a Markov model the most recent state is predicted based on a fixed number of the previous states, and this fixed number of previous states is called the order of the Markov model. At your case, each state could be a different purchase.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.