Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

python - How to store data and plot using the Matplotlib?

I've generated some data from the Markov model, which represents the process of transitioning from one state to the another, in this case I've 4 states. I want to store the recurrence of this four states and then plot them together using Matplotlib. Here is my code


import numpy as np

T = np.array([ [ 0.40, 0.56, 0.03, 0.01],
               [0.45, 0.51, 0.04, 0.00],
               [0.25, 0.25, 0.25, 0.25 ],
               [0.00, 0.00, 0.01, 0.99 ]])

xk = np.arange(len(T))

def gen_sample(state):
    return np.random.choice(xk, 1, p=T[state, :])

initial_state = 0
sample_len = 100
output = [-1 for i in range(sample_len)]
output[0] = initial_state
for i in range(1, sample_len):
    output[i] = gen_sample(output[i-1])[0]
print(output)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...