How to use the hmmlearn.hmm.MultinomialHMM function in hmmlearn

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 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 lpty / nlp_base / segment / src / model.py View on Github external
def get_model(self):
        """
        初始化hmm模型
        """
        model = MultinomialHMM(n_components=len(self.states))
        model.startprob_ = self.init_p
        model.transmat_ = self.trans_p
        model.emissionprob_ = self.emit_p
        return model
github ardaillon / FCN-f0 / prediction.py View on Github external
# uniform prior on the starting pitch
    starting = np.ones(vecSize) / vecSize

    # transition probabilities inducing continuous pitch
    xx, yy = np.meshgrid(range(vecSize), range(vecSize))
    transition = np.maximum(smoothing_factor - abs(xx - yy), 0)
    transition = transition / np.sum(transition, axis=1)[:, None]

    # emission probability = fixed probability for self, evenly distribute the
    # others
    self_emission = 0.1
    emission = (np.eye(vecSize) * self_emission + np.ones(shape=(vecSize, vecSize)) *
                ((1 - self_emission) / vecSize))

    # fix the model parameters because we are not optimizing the model
    model = hmm.MultinomialHMM(vecSize, starting, transition)
    model.startprob_, model.transmat_, model.emissionprob_ = \
        starting, transition, emission

    # find the Viterbi path
    observations = np.argmax(salience, axis=1)
    path = model.predict(observations.reshape(-1, 1), [len(observations)])

    if(modelTag=='CREPE'):
        return np.array([to_local_average_cents_CREPE(salience[i, :], path[i]) for i in
                         range(len(observations))])
    else:
        return np.array([to_local_average_cents(salience[i, :], path[i]) for i in
                         range(len(observations))])
github marl / crepe / crepe / core.py View on Github external
# uniform prior on the starting pitch
    starting = np.ones(360) / 360

    # transition probabilities inducing continuous pitch
    xx, yy = np.meshgrid(range(360), range(360))
    transition = np.maximum(12 - abs(xx - yy), 0)
    transition = transition / np.sum(transition, axis=1)[:, None]

    # emission probability = fixed probability for self, evenly distribute the
    # others
    self_emission = 0.1
    emission = (np.eye(360) * self_emission + np.ones(shape=(360, 360)) *
                ((1 - self_emission) / 360))

    # fix the model parameters because we are not optimizing the model
    model = hmm.MultinomialHMM(360, starting, transition)
    model.startprob_, model.transmat_, model.emissionprob_ = \
        starting, transition, emission

    # find the Viterbi path
    observations = np.argmax(salience, axis=1)
    path = model.predict(observations.reshape(-1, 1), [len(observations)])

    return np.array([to_local_average_cents(salience[i, :], path[i]) for i in
                     range(len(observations))])
github Lab-Work / gpsresilience / hmm_event_detection.py View on Github external
# The symbols array contains "1" if there is an outlier, "0" if there is not
    symbols = []
    for i in range(len(mahal_list)):
        if(mahal_list[i] > threshold or c_list[i]==1):
            symbols.append(1)
        else:
            symbols.append(0)
    
    

  
    
    # Actually set up the hmm
    model = MultinomialHMM(n_components=2, transmat=trans_matrix, startprob=initial_state)
    model.emissionprob_ = emission_matrix
    
    # Make the predictions
    lnl, predictions = model.decode(symbols)
    
    events = get_all_events(predictions, sorted_dates, mahal_list, global_pace_list,
                            expected_pace_list)
    
    # Sort events by duration, starting with the long events
    events.sort(key = lambda x: x[2], reverse=True)
    return events, predictions