этот код работает отлично.Мне просто нужна помощь, чтобы разместить комбинацию значений кортежа матрицы в столбце, а также в строках:
from __future__ import division
import seaborn as sns; sns.set()
def transition_matrix(transitions):
states = 1+ max(transitions) #number of states
MAT = [[0]*states for _ in range(states)] #placeholder to shape the matrix based on states
#print('mat', M)
for (i,j) in zip(transitions,transitions[1:]):
#print(i, j)
"""matrix with transition from state i to state j"""
MAT[i][j] += 1
#print("matrix with transition",M)
for row in MAT:
"""calculating probabilities"""
s = sum(row)
if s > 0:
row[:] = [f/s for f in row]
return MAT
#test:
employeeCountperEmployer = [1, 2, 3, 1, 4, 2, 1]
m = transition_matrix(employeeCountperEmployer)
#print(m)
for row in m:
print('|'.join('{0:.2f}'.format(x) for x in row))
, это генерирует следующее:
0.00|0.00|0.00|0.00|0.00
0.00|0.00|0.50|0.00|0.50
0.00|0.50|0.00|0.50|0.00
0.00|1.00|0.00|0.00|0.00
0.00|0.00|1.00|0.00|0.00
Однако я хотел это как
1 2 3 4
1 0.00|0.00|0.00|0.00|0.00
2 0.00|0.00|0.50|0.00|0.50
3 0.00|0.50|0.00|0.50|0.00
4 0.00|1.00|0.00|0.00|0.00
0.00|0.00|1.00|0.00|0.00