In [3]:
    
% matplotlib inline
import pylab
import numpy as np
import pandas as pd
from hmmlearn.hmm import GaussianHMM
    
In [4]:
    
df = pd.read_csv("speed.csv", sep = ",")
df.head(5)
    
    Out[4]:
Drop the row number and "corr" so we can focus on the influence of "prev" and "Pacc" on "rt." Also define "prev" as a factor.
In [5]:
    
x = df.drop(["row", "corr"], axis = 1)
x["prev"] = pd.factorize(x["prev"])[0]
    
In [6]:
    
model = GaussianHMM(n_components=2, n_iter=10000, random_state=1).fit(x)
model.monitor_
    
    Out[6]:
Predict the hidden state for each record and get count of predicted states.
In [7]:
    
states = model.predict(x)
pd.Series(states).value_counts()
    
    Out[7]:
Get the mean reaction time (rt) for each of the two states.
In [8]:
    
model.means_[:, 0]
    
    Out[8]:
In [9]:
    
fig = pylab.figure(figsize=(20, 1))
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_xlabel("Record number")
ax.set_ylabel("State")
ax.plot(states)
    
    Out[9]:
    
In [ ]: