📅  最后修改于: 2022-03-11 14:46:43.206000             🧑  作者: Mango
def compute_transition_matrix2(data, n, step = 1):
t = np.array(data)
step = step
total_inds = t.size - (step + 1) + 1
t_strided = np.lib.stride_tricks.as_strided(
t,
shape = (total_inds, 2),
strides = (t.strides[0], step * t.strides[0]))
inds, counts = np.unique(t_strided, axis = 0, return_counts = True)
P = np.zeros((n, n))
P[inds[:, 0], inds[:, 1]] = counts
sums = P.sum(axis = 1)
# Avoid divide by zero error by normalizing only non-zero rows
P[sums != 0] = P[sums != 0] / sums[sums != 0][:, None]
# P = P / P.sum(axis = 1)[:, None]
return P
print(compute_transition_matrix2([3, 0, 1, 3, 2, 6, 5, 4, 7, 5, 4], 8, 1))
# data is the input time series data, n is the total number of states in the Markov chain, step is the transition step.