How to use hmmlearn - 10 common examples

To help you get started, we’ve selected a few hmmlearn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GenTang / intro_ds / ch09-generative_models / gaussian_hmm / stock_analysis.py View on Github external
def getHiddenStatus(data):
    """
    使用Gaussian HMM对数据进行建模,并得到预测值
    """
    cols = ["r_5", "r_20", "a_5", "a_20"]
    model = GaussianHMM(n_components=3, covariance_type="full", n_iter=1000,
        random_state=2010)
    model.fit(data[cols])
    hiddenStatus = model.predict(data[cols])
    return hiddenStatus
github CostaLab / reg-gen / rgt / HINT / Main.py View on Github external
else:
                        group.hmm = hmm_data.get_default_hmm_dnase_histone()

    # Creating scikit HMM list
    for group in group_list:

        if (group.flag_multiple_hmms):

            hmm_list = []
            for hmm_file_name in group.hmm:

                try:
                    hmm_scaffold = HMM()
                    hmm_scaffold.load_hmm(hmm_file_name)
                    if (int(hmm_ver.split(".")[0]) <= 0 and int(hmm_ver.split(".")[1]) <= 1):
                        scikit_hmm = GaussianHMM(n_components=hmm_scaffold.states, covariance_type="full",
                                                 transmat=array(hmm_scaffold.A), startprob=array(hmm_scaffold.pi))
                        scikit_hmm.means_ = array(hmm_scaffold.means)
                        scikit_hmm.covars_ = array(hmm_scaffold.covs)
                    else:
                        scikit_hmm = GaussianHMM(n_components=hmm_scaffold.states, covariance_type="full")
                        scikit_hmm.startprob_ = array(hmm_scaffold.pi)
                        scikit_hmm.transmat_ = array(hmm_scaffold.A)
                        scikit_hmm.means_ = array(hmm_scaffold.means)
                        scikit_hmm.covars_ = array(hmm_scaffold.covs)

                except Exception:
                    error_handler.throw_error("FP_HMM_FILES")
                hmm_list.append(scikit_hmm)

            group.hmm = hmm_list
github duoergun0729 / 1book / code / 9-3.py View on Github external
def train_hmm(domain_list):
    X = [[0]]
    X_lens = [1]
    for domain in domain_list:
        ver=domain2ver(domain)
        np_ver = np.array(ver)
        X=np.concatenate([X,np_ver])
        X_lens.append(len(np_ver))

    remodel = hmm.GaussianHMM(n_components=N, covariance_type="full", n_iter=100)
    remodel.fit(X,X_lens)
    joblib.dump(remodel, FILE_MODEL)

    return remodel
github nilmtk / nilmtk / nilmtk / disaggregate / fhmm_exact.py View on Github external
continue
                
            assert X.ndim == 2
            self.X = X

            if self.num_of_states > 0:
                 # User has specified the number of states for this appliance
                 num_total_states = self.num_of_states

            else:
                 # Find the optimum number of states
                states = cluster(meter_data, max_num_clusters)
                num_total_states = len(states)

            print("Training model for submeter '{}'".format(appliance))
            learnt_model[appliance] = hmm.GaussianHMM(num_total_states, "full")

            # Fit
            learnt_model[appliance].fit(X)
            print("Learnt model for : "+appliance)

            # Check to see if there are any more chunks.
            # TODO handle multiple chunks per appliance.

        # Combining to make a AFHMM
        self.meters = []
        new_learnt_models = OrderedDict()
        for meter in learnt_model:
            print(meter)
            startprob, means, covars, transmat = sort_learnt_parameters(
                learnt_model[meter].startprob_, learnt_model[meter].means_,
                learnt_model[meter].covars_, learnt_model[meter].transmat_)
github mil-tokyo / NeuralMelody / chord_sequence_generation.py View on Github external
#################################
  # Group by song
  parts_length = []
  chords_length = []
  start_ind = 0
  for end_ind in song_indices:
    chords_length.append(end_ind - start_ind + 1)
    start_ind = end_ind + 1
  parts_length = [e/2 for e in chords_length]
  ##################################################################
  
  ##################################################################
  # PARTS
  # Compute HMM for part modeling
  hmm_part = MultinomialHMM(n_components=nh_part, n_iter=20)
  hmm_part.fit(parts_data, parts_length)

  # def plot_mat(matrix, name):
  #   fig = plt.figure()
  #   ax = fig.add_subplot(1,1,1)
  #   ax.set_aspect('equal')
  #   plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean)
  #   plt.colorbar()
  #   plt.savefig(name, format='pdf')

  # plot_mat(hmm_part.transmat_, 'part_transmat.pdf')
  # plot_mat(np.reshape(hmm_part.startprob_, [-1, 1]), 'part_startprob.pdf')
  # plot_mat(hmm_part.emissionprob_, 'part_emissionprob.pdf')
  ##################################################################
  
  ##################################################################
github feynmanliang / bachbot / scripts / baseline.py View on Github external
def multinomial_hmm():
    "Trains a discrete emission HMM. Uses ./scratch/concat_corpus.txt"
    # TODO: This is too slow currently even with only 1000 samples
    np.random.seed(42)

    corpus = filter(lambda x: x != u'\n', codecs.open(SCRATCH_DIR + '/concat_corpus.txt', "r", "utf-8").read())[:1000]
    char_to_idx = { c:i for i,c in enumerate(set(corpus)) }

    ohe = OneHotEncoder(dtype=np.int, sparse=False)
    corpus_enc = ohe.fit_transform([[char_to_idx[c]] for c in corpus])

    model = hmm.MultinomialHMM(n_components=len(char_to_idx), verbose=True)
    model.fit(corpus_enc)

    pickle.dump(model, open('hmm.model', 'wb'))
github TheKingOfDuck / logonTracer / logontracer.py View on Github external
# udata = udata[(udata*np.sign(abs(np.diff(np.concatenate(([0], udata)))))).nonzero()]
                if udata.shape[0] > 2:
                    data_array = np.append(data_array, udata)
                    lengths.append(udata.shape[0])

        stime += datetime.timedelta(days=1)
        if frame.loc[(frame["date"].str.contains(date))].empty:
            break

    data_array[data_array == 4776] = 0
    data_array[data_array == 4768] = 1
    data_array[data_array == 4769] = 2
    data_array[data_array == 4624] = 3
    data_array[data_array == 4625] = 4
    # model = hmm.GaussianHMM(n_components=3, covariance_type="full", n_iter=10000)
    model = hmm.MultinomialHMM(n_components=3, n_iter=10000)
    # model.startprob_ = start_probability
    model.emissionprob_ = emission_probability
    model.fit(np.array([data_array], dtype="int").T, lengths)
    joblib.dump(model, FPATH + "/model/hmm.pkl")
github happyflyingfish / cs-skill-tree / machine learning / hidden markov model / untitled folder / 1. Buiding HMM and generating samples.py View on Github external
__author__ = 'conghuai'

import numpy as np
from hmmlearn import hmm

np.random.seed(42)
model = hmm.GaussianHMM(n_components=3, covariance_type='full')
model.startprob_ = np.array([0.6, 0.3, 0.1])
model.transmat_ = np.array([[0.7, 0.2, 0.1],
                           [0.3, 0.5, 0.2],
                           [0.3, 0.3, 0.4]])
model.means_ = np.array([[0.0, 0.0], [3.0, -3.0], [5.0, 10.0]])
model.covars_ = np.tile(np.identity(2), (3, 1, 1))
X, Z = model.sample(100)

remodel = hmm.GaussianHMM(n_components=3, covariance_type='full', n_iter=100)
remodel.fit(X)
Z2 = remodel.predict(X)
github ringw / homer / moonshine / models / train_barlines_hmm.py View on Github external
def fit(files):
    HMM_FILE = os.path.join(os.path.dirname(__file__), 'barlines_hmm.pkl')

    model = hmm.BernoulliMultiHMM(n_components=100)

    staves = []
    for filename in files:
        try:
            pages = moonshine.open(filename)
        except Exception:
            import traceback
            traceback.print_exc()
            continue
        if not (0 < len(pages) <= 100):
            continue
        print filename
        for page in pages:
            gc.collect()
            page.preprocess()
            if type(page.staff_dist) is not int:
github ringw / homer / moonshine / models / train_barlines_bernoulli.py View on Github external
def fit(files):
    HMM_FILE = os.path.join(os.path.dirname(__file__), 'barlines_hmm_bernoulli10.pkl')
    STAVES_FILE = os.path.join(os.path.dirname(__file__), 'unlabeled_barlines.zip')
    NUM_STAVES = 100

    model = hmm.BernoulliHMM(n_components=10)
    staves = []
    staves_zip = zipfile.ZipFile(STAVES_FILE, 'r')
    i = 0
    for staves_name in staves_zip.namelist():
        staff_bits = np.fromstring(staves_zip.open(staves_name,'r').read(), np.uint8)
        staff_bytes = np.unpackbits(staff_bits)
        staves.append(staff_bytes.reshape((20, -1)).T.astype(bool))
        i += 1
        if i >= NUM_STAVES:
            break
    print len(staves), 'staves'

    model.fit(staves)
    cPickle.dump(model, open(HMM_FILE, 'wb'))