Пример #1
0
    def test_fit(self, params='stmwc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GMMHMM(self.n_components, covars_prior=1.0)
        h.startprob_ = self.startprob
        h.transmat_ = hmm.normalize(
            self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
        h.gmms_ = self.gmms_

        # Create training data by sampling from the HMM.
        train_obs = [h.sample(n=10, random_state=self.prng)[0]
                     for x in range(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.n_iter = 0
        h.fit(train_obs)
        h.transmat_ = hmm.normalize(self.prng.rand(self.n_components,
                                                   self.n_components), axis=1)
        h.startprob_ = hmm.normalize(self.prng.rand(self.n_components))

        trainll = train_hmm_and_keep_track_of_log_likelihood(
            h, train_obs, n_iter=n_iter, params=params)[1:]

        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll)))

        # XXX: this test appears to check that training log likelihood should
        # never be decreasing (up to a tolerance of 0.5, why?) but this is not
        # the case when the seed changes.
        raise SkipTest("Unstable test: trainll is not always increasing "
                       "depending on seed")

        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Пример #2
0
    def test_fit(self, params='stmwc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GMMHMM(self.n_components)
        h.startprob = self.startprob
        h.transmat = hmm.normalize(
            self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
        h.gmms = self.gmms

        # Create training data by sampling from the HMM.
        train_obs = [h.rvs(n=10, random_state=self.prng) for x in xrange(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.fit(train_obs, n_iter=0)
        h.transmat = hmm.normalize(self.prng.rand(self.n_components,
                                                  self.n_components),
                                   axis=1)
        h.startprob = hmm.normalize(self.prng.rand(self.n_components))

        trainll = train_hmm_and_keep_track_of_log_likelihood(h,
                                                             train_obs,
                                                             n_iter=n_iter,
                                                             params=params,
                                                             covars_prior=1.0,
                                                             **kwargs)[1:]
        if not np.all(np.diff(trainll) > 0) and verbose:
            print
            print 'Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll))
        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Пример #3
0
 def test_sample(self, n=1000):
     h = hmm.GMMHMM(self.n_components,
                    self.covariance_type,
                    startprob=self.startprob,
                    transmat=self.transmat,
                    gmms=self.gmms)
     samples = h.sample(n)[0]
     self.assertEquals(samples.shape, (n, self.n_features))
Пример #4
0
 def test_rvs(self, n=1000):
     h = hmm.GMMHMM(self.n_components,
                    self.cvtype,
                    startprob=self.startprob,
                    transmat=self.transmat,
                    gmms=self.gmms)
     samples = h.rvs(n)
     self.assertEquals(samples.shape, (n, self.n_features))
Пример #5
0
    def test_fit_works_on_sequences_of_different_length(self):
        obs = [self.prng.rand(3, self.n_features),
               self.prng.rand(4, self.n_features),
               self.prng.rand(5, self.n_features)]

        h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)
        # This shouldn't raise
        # ValueError: setting an array element with a sequence.
        h.fit(obs)
Пример #6
0
    def test_eval_and_decode(self):
        h = hmm.GMMHMM(self.n_components, gmms=self.gmms)
        # Make sure the means are far apart so posteriors.argmax()
        # picks the actual component used to generate the observations.
        for g in h.gmms:
            g.means_ *= 20

        refstateseq = np.repeat(range(self.n_components), 5)
        nobs = len(refstateseq)
        obs = [h.gmms[x].sample(1).flatten() for x in refstateseq]

        ll, posteriors = h.eval(obs)

        self.assertEqual(posteriors.shape, (nobs, self.n_components))
        assert_array_almost_equal(posteriors.sum(axis=1), np.ones(nobs))

        viterbi_ll, stateseq = h.decode(obs)
        assert_array_equal(stateseq, refstateseq)
Пример #7
0
    def test_attributes(self):
        h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)

        self.assertEquals(h.n_components, self.n_components)

        h.startprob = self.startprob
        assert_array_almost_equal(h.startprob, self.startprob)
        self.assertRaises(ValueError, h._set_startprob, 2 * self.startprob)
        self.assertRaises(ValueError, h._set_startprob, [])
        self.assertRaises(ValueError, h._set_startprob,
                          np.zeros((self.n_components - 2, self.n_features)))

        h._set_transmat(self.transmat)
        assert_array_almost_equal(h._get_transmat(), self.transmat)
        self.assertRaises(ValueError, h._set_transmat, 2 * self.transmat)
        self.assertRaises(ValueError, h._set_transmat, [])
        self.assertRaises(ValueError, h._set_transmat,
                          np.zeros((self.n_components - 2, self.n_components)))
Пример #8
0
    def test_attributes(self):
        h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)

        self.assertEquals(h.n_components, self.n_components)

        h.startprob_ = self.startprob
        assert_array_almost_equal(h.startprob_, self.startprob)
        self.assertRaises(ValueError, h.__setattr__, 'startprob_',
                          2 * self.startprob)
        self.assertRaises(ValueError, h.__setattr__, 'startprob_', [])
        self.assertRaises(ValueError, h.__setattr__, 'startprob_',
                          np.zeros((self.n_components - 2, self.n_features)))

        h.transmat_ = self.transmat
        assert_array_almost_equal(h.transmat_, self.transmat)
        self.assertRaises(ValueError, h.__setattr__, 'transmat_',
                          2 * self.transmat)
        self.assertRaises(ValueError, h.__setattr__, 'transmat_', [])
        self.assertRaises(ValueError, h.__setattr__, 'transmat_',
                          np.zeros((self.n_components - 2, self.n_components)))
Пример #9
0
from sklearn import hmm

models = {
    'GMMHMM_5mix_5states_vitals_HR': {
        'NSTATES': 5,
        'MODEL': hmm.GMMHMM(n_components=5, n_mix=5, covariance_type='full'),
        'NMIXTURES': 5,
        'DT': 0.5,
        'max_time': None,
        'min_time': None,
        'do_smoothing': False,
        'do_imputation': False,
        'vital_signs_to_use': ['Heart Rate (beats/min)']
    },
    'GMMHMM_3mix_3states_vitals_HR': {
        'NSTATES': 3,
        'MODEL': hmm.GMMHMM(n_components=3, n_mix=3, covariance_type='full'),
        'NMIXTURES': 3,
        'DT': 0.5,
        'max_time': None,
        'min_time': None,
        'do_smoothing': False,
        'do_imputation': False,
        'vital_signs_to_use': ['Heart Rate (beats/min)'],
    },
    'GMMHMM_5mix_5states_vitals_HR_tmin12hrs': {
        'NSTATES': 5,
        'MODEL': hmm.GMMHMM(n_components=5, n_mix=5, covariance_type='full'),
        'NMIXTURES': 5,
        'DT': 0.5,
        'max_time': None,