Пример #1
0
def train_HMM(n_components,
              observations,
              lengths,
              labels,
              verbose=False,
              cov_type='spherical',
              iter=500):
    AD_idx = np.where(labels == 1)
    AD_lengths = lengths[AD_idx[0]]

    nonAD_idx = np.where(labels != 1)
    nonAD_lengths = lengths[nonAD_idx[0]]

    l = np.nditer(labels)
    nonAD_observations = np.zeros((0, observations.shape[1]))
    AD_observations = np.zeros((0, observations.shape[1]))
    for s, e, in iter_from_X_lengths(observations, lengths):
        temp = observations[s:e, :]
        if l.next() == 0:
            nonAD_observations = np.vstack((nonAD_observations, temp))
        else:
            AD_observations = np.vstack((AD_observations, temp))

    AD_hmm = cf.setup_and_train(n_components, AD_observations, AD_lengths,
                                cov_type, verbose, iter)

    nonAD_hmm = cf.setup_and_train(n_components, nonAD_observations,
                                   nonAD_lengths, cov_type, verbose, iter)

    return AD_hmm, nonAD_hmm
Пример #2
0
def train_HMM(n_components, train_obs, train_len, test_obs, test_len, verbose=False,
              cov_type='spherical', iter=500):

    h0 = cf.setup_and_train(n_components, train_obs, train_len, cov_type,
                            verbose, iter)
    train_state_seq = h0.predict(train_obs, train_len)
    train_seq = cf.vecs2mats(train_state_seq, train_len)

    test_state_seq = h0.predict(test_obs, test_len)
    test_seq = cf.vecs2mats(test_state_seq, test_len)

    """ Convert sequences to transition matrices """
    train_transitions = np.zeros((len(train_len), n_components * n_components))
    train_sequences = train_seq.astype(int)

    for i, state_seq in enumerate(train_sequences):
        temp = np.zeros((n_components, n_components))
        for step in np.arange(1, train_len[i]):
            temp[train_sequences[i, step - 1], train_sequences[i, step]] += 1
        train_transitions[i, :] = temp.flatten()

    test_transitions = np.zeros((len(test_len), n_components * n_components))
    test_sequences = test_seq.astype(int)

    for i, state_seq in enumerate(test_sequences):
        temp = np.zeros((n_components, n_components))
        for step in np.arange(1, test_len[i]):
            temp[test_sequences[i, step - 1], test_sequences[i, step]] += 1
        test_transitions[i, :] = temp.flatten()

    return train_transitions, test_transitions
Пример #3
0
def train_HMM(n_comp,
              train_obs,
              train_len,
              observations,
              lengths,
              verbose=True,
              cov_type='spherical',
              iter=500):
    h0 = cf.setup_and_train(n_comp, train_obs, train_len, cov_type, verbose,
                            iter)

    sequences = h0.predict(observations, lengths)
    seq = cf.vec2mat(sequences, lengths)

    return seq
Пример #4
0
def train_HMM(n_components,
              observations,
              lengths,
              verbose=False,
              cov_type='spherical',
              iter=500,
              params="mcts"):

    h0 = cf.setup_and_train(n_components,
                            observations,
                            lengths,
                            cov_type,
                            verbose,
                            iter,
                            params=params)
    return h0
Пример #5
0
def train_HMM(n_components,
              train_obs,
              train_len,
              test_obs,
              test_len,
              verbose=False,
              cov_type='spherical',
              iter=500):

    h0 = cf.setup_and_train(n_components, train_obs, train_len, cov_type,
                            verbose, iter)
    train_state_seq = h0.predict(train_obs, train_len)
    train_seq = cf.vecs2mats(train_state_seq, train_len)

    test_state_seq = h0.predict(test_obs, test_len)
    test_seq = cf.vecs2mats(test_state_seq, test_len)

    return train_seq, test_seq