1
votes

I am looking for a way to compute a Markov transition matrix from a customer transactions list of an ecommerce website.

Basically I would need a nxn matrix with n as the number of purchased products, and in each row there would be the probability of let's say, purchasing product 1 , I have X probability of purchasing product 2, y probability of purchasing product 1 again, and so on. We can assume the initial state to be an array with a 1 on product 1 and a 0 for all the other products (we just bought product 1 for now).

Is there some sort of python package able to compute the transition matrix probabilities for me feeding the purchase data? In all the examples I looked at people were just feeding a pre computed matrix.

Thanks in advance

1

1 Answers

0
votes

As a first step, you can use markovchain package. You can find more details about this package here.You can install it using pip install markovchain and then compute the transition matrix by training a text base Markov model. For example:

from markovchain.text import MarkovText, ReplyMode

markov = MarkovText()

with open('data.txt') as fp:
    markov.data(fp.read())

with open('data2.txt') as fp:
    for line in fp:
        markov.data(line, part=True)
markov.data('', part=False)

print(markov())
print(markov(max_length=16, reply_to='sentence start', reply_mode=ReplyMode.END))

markov.save('markov.json')

markov = MarkovText.from_file('markov.json')