“Konversi deret waktu menjadi matriks transisi” Kode Jawaban

Konversi deret waktu menjadi matriks transisi

def compute_transition_matrix(data, n, step = 1):
    P = np.zeros((n, n))
    m = len(data)
    for i in range(m):
        initial, final = i, i + step
        if final < m:
            P[data[initial]][data[final]] += 1
    sums = np.sum(P, axis = 1)
    for i in range(n):
        for j in range(n):
            P[i][j] = P[i][j] / sums[i]
    return P

print(compute_transition_matrix([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.
Enchanting Eel

Konversi deret waktu menjadi matriks transisi

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.
Enchanting Eel

Jawaban yang mirip dengan “Konversi deret waktu menjadi matriks transisi”

Pertanyaan yang mirip dengan “Konversi deret waktu menjadi matriks transisi”

Lebih banyak jawaban terkait untuk “Konversi deret waktu menjadi matriks transisi” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya