I am having problem with hidden_markov package when applying a slight change in a simple example in its documentation. In the following code I try 2 states and 3 possible observations (in the documentation's example there are 2 possible observations and the code works fine):
states = ('s', 't')
possible_observation = ('A', 'B', 'C')
# Numpy arrays of the data
start_probability = np.matrix( '0.5 0.5')
transition_probability = np.matrix('0.6 0.4 ; 0.3 0.7')
emission_probability = np.matrix( '0.3 0.2 0.5 ; 0.3 0.1 0.6')
# Initialize class object
test = hmm(states,possible_observation,
start_probability,
transition_probability,
emission_probability)
observations = ('A', 'B','B','A', 'C')
obs4 = ('B', 'C', 'A','B')
observation_tuple = []
observation_tuple.extend( [observations,obs4] )
quantities_observations = [18, 28]
num_iter=1000
e,t,s = test.train_hmm(observation_tuple,num_iter,quantities_observations)
After running the code I get the error:
ValueError: operands could not be broadcast together with shapes (2,3) (1,2)
Interestingly, when I try 3 states and 3 possible observations (and modify the probability matrices based on this change) the code works fine. Either I am missing something, or the number of states and possible observations should always be equal which does not make sense.
Based on the matrix you have provided above, there are only two matrices that have the shape (2,3)
and (1,2)
which are emission_probability
and start_probability
respectively and the error is of the matrix dimension mismatch. To match the matrix dimensions so that they can be used for calculation of dot product is by doing.
emission_probability = emission_probability.T
start_probability = start_probability.T
This step has to be done before initializing the class object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.