I am new to Hidden Markov Models, and to experiment with it I am studying the scenario of sunny/rainy/foggy weather based on the observation of a person carrying or not an umbrella, with the help of the hmmlearn package in Python. The data used in my tests was obtained from this page (the test and output files of "test 1").
I created the simple code presented bellow to fit an unsupervised HMM from the test data, and then compared the prediction to the expected output. The results seem pretty good (7 out of 10 correct predictions).
My question is: how am I supposed to know the mapping of the hidden states handled by the model to the real states in the problem domain? (in other words, how do I relate the responses to the desired states of my problem domain?)
This might be a very naïve question, but if the model was supervised I would understand that the mapping is given by me when providing the Y values for the fit method... yet I simply can't figure out how it works in this case.
Code:
import numpy as np
from hmmlearn import hmm
# Load the data from a CSV file
data = np.genfromtxt('training-data.csv', skip_header=1, delimiter=',',
dtype=str)
# Hot encode the 'yes' and 'no' categories of the observation
# (i.e. seeing or not an umbrella)
x = np.array([[1, 0] if i == 'yes' else [0, 1] for i in data[:, 1]])
# Fit the HMM from the data expecting 3 hidden states (the weather on the day:
# sunny, rainy or foggy)
model = hmm.GaussianHMM(n_components=3, n_iter=100, verbose=True)
model.fit(x, [len(x)])
# Test the model
test = ['no', 'no', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'yes']
x_test = np.array([[1, 0] if i == 'yes' else [0, 1] for i in test])
y_test = ['foggy', 'foggy', 'foggy', 'rainy', 'sunny', 'foggy', 'rainy', 'rainy', 'foggy', 'rainy']
y_pred = model.predict(x_test)
mp = {0: 'sunny', 1: 'rainy', 2: 'foggy'} # THIS IS MY ASSUMPTION
print('\n\n\n')
print('Expected:')
print(y_test)
print('Predicted:')
print([mp[i] for i in y_pred])
Result:
Expected: ['foggy', 'foggy', 'foggy', 'rainy', 'sunny', 'foggy', 'rainy', 'rainy', 'foggy', 'rainy'] Predicted: ['foggy', 'foggy', 'sunny', 'rainy', 'foggy', 'sunny', 'rainy', 'rainy', 'foggy', 'rainy']