Пример #1
0
        'emission_state':np.array([1,2,3,4,5]),
        'init_prob':np.array([0.5,0.5]),
        'trans_prob':np.array([[0.8,0.2],[0.1,0.9]]),
        'emission_prob_type':'matrix',
        'emission_matrix':np.array([[0.2,0.5,0.2,0.1,0],[0,0.1,0.4,0.4,0.1]])
    }
    hmm=HMM(init_params=params)
    hmm.write_params_to_file('params')

    # params read
    hmm=HMM(folder_name='params')

    # generate
    hidden_state_chain,emitted_state_chain=hmm.generate(115)
    hidden_state_chain_,emitted_state_chain_=hidden_state_chain,emitted_state_chain
    hidden_state_chain,emitted_state_chain=hmm.index2state(hidden_state_chain,'hidden_state'),hmm.index2state(emitted_state_chain,'emission_state')
    
    # plot the generated result
    fig, ax = plt.subplots()
    x=np.arange(115)
    ax.plot(x,emitted_state_chain,label='emitted state')
    ax.plot(x,hidden_state_chain,label='hidden state')
    ax.set_xlabel('position index')
    ax.set_ylabel('state')
    ax.legend()
    fig.savefig('result/q1.pdf')

    fig, ax = plt.subplots()
    x=np.arange(115)
    ax.plot(x,emitted_state_chain,'.',label='emitted state')
    ax.plot(x,hidden_state_chain,'.',label='hidden state')