I have modified some Python code found, which calculates the transition matrix for an observed sequence (transitions named here).
For some reason it works fine, but when I put a number above or equal to "10" I get this error:
"ord() expected a character, but string of length 2 found"
states_no = 10
transitions = [['10', '1', '1', '2', '3', '1']]
trans_matr = []
for k in range(len(transitions)):
def rank(c):
return ord(c) - ord('1')
T = [rank(c) for c in transitions[k]]
#print(transitions[k])
#create matrix of zeros
M = [[0]*states_no for _ in range(states_no)]
for (i,j) in zip(T,T[1:]):
M[i][j] += 1
#now convert to probabilities:
for row in M:
n = sum(row)
if n > 0:
row[:] = [f/sum(row) for f in row]
#print M:
trans_matr.append(M)
#for row in M:
#print(row)
#print(trans_matr)
trans_matr_array=numpy.array(trans_matr)
print(trans_matr_array)
I guess it has something to do with the character recognition. Is there any way to put numbers up to 14 without getting this error?
As far as I can tell the code only uses ord()
for calculating the difference between the input and 1
. Does your input include non-numeric characters?
If not, simply change the function "rank":
def rank(c)
return int(c) - 1
I assume you only need integers, you can change it to float to accept any number.
If you need to also accept non-numeric characters:
def rank(c):
if c.isnumeric():
return int(c) - 1
elif len(c) == 1:
return ord(c) - ord('1')
else:
raise TypeError('non numeric inputs should have length 1')