Counting repeated sequences in transition table

Viewed 45

I'm using the following function to generate a transition table:

import numpy as np
import pandas as pd
def make_table(allSeq):
 n = max([ max(s) for s in allSeq ]) + 1
 arr = np.zeros((n,n), dtype=int)
 for seq in allSeq:
  ind = (seq[1:], seq[:-1])
  arr[ind] += 1
 return pd.DataFrame(arr).rename_axis(index='Next', columns='Current')

However, my result is incorrect:

list1 = [1,2,3,4,5,4,5,4,5]
list2 = [4,5,4,5]
make_table([list1, list2])

Current  0  1  2  3  4  5
Next                     
0        0  0  0  0  0  0
1        0  0  0  0  0  0
2        0  1  0  0  0  0
3        0  0  1  0  0  0
4        0  0  0  1  0  2
5        0  0  0  0  2  0

For example, the transition 4->5 should be counted 5 times, but it's only counted once per sequence (2). I know the issue is the arr[ind] += 1 line, but I just can't figure it out! Do I nest another loop, or is there a slick way to add the total number of instances at once? Thanks!

2 Answers

Figured it out! Switched to the following:

def make_table(allSeq):
 n = max([ max(s) for s in allSeq ]) + 1
 arr = np.zeros((n,n), dtype=int)
 for seq in allSeq:
  for i,j in zip(seq[1:],seq[:-1]):
   ind = (i,j)
   arr[ind] += 1
 return pd.DataFrame(arr).rename_axis(index='Next', columns='Current')

Another loop seems like the easiest solution, with a bit of a twist of using zip:

import numpy as np
import pandas as pd
def make_table(allSeq):
    n = max([ max(s) for s in allSeq ]) + 1
    arr = np.zeros((n,n), dtype=int)
    for seq in allSeq:
        ind = zip(seq[1:], seq[:-1])
        for i in ind:
            arr[i] += 1
    return pd.DataFrame(arr).rename_axis(index='Next', columns='Current')
list1 = [1,2,3,4,5,4,5,4,5]
list2 = [4,5,4,5]
make_table([list1, list2])

returns

  Next    0    1    2    3    4    5
------  ---  ---  ---  ---  ---  ---
     0    0    0    0    0    0    0
     1    0    0    0    0    0    0
     2    0    1    0    0    0    0
     3    0    0    1    0    0    0
     4    0    0    0    1    0    3
     5    0    0    0    0    5    0
Related