How to use the hmmlearn.hmm.GaussianHMM 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 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 PacktPublishing / Python-Machine-Learning-Cookbook / Chapter07 / speech_recognizer.py View on Github external
def __init__(self, model_name='GaussianHMM', n_components=4, cov_type='diag', n_iter=1000):
        self.model_name = model_name
        self.n_components = n_components
        self.cov_type = cov_type
        self.n_iter = n_iter
        self.models = []

        if self.model_name == 'GaussianHMM':
            self.model = hmm.GaussianHMM(n_components=self.n_components, 
                    covariance_type=self.cov_type, n_iter=self.n_iter)
        else:
            raise TypeError('Invalid model type')
github CostaLab / reg-gen / rgt / HINT / Footprinting.py View on Github external
hmm_data = HmmData()

    ###################################################################################################
    # Creating HMMs
    ###################################################################################################
    hmm_file = None
    if args.hmm_file:
        hmm_file = args.hmm_file
    else:
        hmm_file = hmm_data.get_default_hmm_histone()

    scikit_hmm = None
    try:
        hmm_scaffold = HMM()
        hmm_scaffold.load_hmm(hmm_file)
        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)
        scikit_hmm._compute_log_likelihood = types.MethodType(_compute_log_likelihood, scikit_hmm)
    except Exception:
        err.throw_error("FP_HMM_FILES")

    initial_clip = 1000 if not args.initial_clip else args.initial_clip
    sg_window_size = 201 if not args.sg_window_size else args.sg_window_size
    norm_per = 98 if not args.norm_per else args.norm_per
    slope_per = 98 if not args.slope_per else args.slope_per
    downstream_ext = 200 if not args.downstream_ext else args.downstream_ext
    upstream_ext = 0 if not args.upstream_ext else args.upstream_ext
    region_total_ext = 10000 if not args.region_total_ext else args.region_total_ext
    forward_shift = 0 if not args.forward_shift else args.forward_shift
github MikeMpapa / CNNs-Speech-Music-Discrimination / ClassifyWav.py View on Github external
flagsNew = []
            for j, fl in enumerate(flagsIndGT):      # append features and labels
                flagsNew.append(classesAll.index(classesAllGT[flagsIndGT[j]]))

            flagsAll = np.append(flagsAll, np.array(flagsNew))
            
            if ifile == 0:
                Fall = P
            else:
                Fall = np.concatenate((Fall, P), axis=0)

            print Fall.shape
            print flagsAll.shape

    startprob, transmat, means, cov = audioSegmentation.trainHMM_computeStatistics(Fall.T, flagsAll)        # compute HMM statistics
    hmm = hmmlearn.hmm.GaussianHMM(startprob.shape[0], "diag")                          # train HMM
    hmm.startprob_ = startprob
    hmm.transmat_ = transmat        
    hmm.means_ = means
    hmm.covars_ = cov

    fo = open(outputmodelName, "wb")   # save HMM model
    cPickle.dump(hmm, fo, protocol=cPickle.HIGHEST_PROTOCOL)
    cPickle.dump(classesAll, fo, protocol=cPickle.HIGHEST_PROTOCOL)
    fo.close()

    return hmm, classesAll
github CostaLab / reg-gen / rgt / HINT / Footprinting.py View on Github external
###################################################################################################
    # Creating HMMs
    ###################################################################################################
    if args.hmm_file:
        hmm_file = args.hmm_file
    else:
        if args.bias_correction:
            hmm_file = hmm_data.get_default_hmm_dnase_histone_bc()
        else:
            hmm_file = hmm_data.get_default_hmm_dnase_histone()
    scikit_hmm = None
    try:
        hmm_scaffold = HMM()
        hmm_scaffold.load_hmm(hmm_file)
        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)
        scikit_hmm._compute_log_likelihood = types.MethodType(_compute_log_likelihood, scikit_hmm)
    except Exception:
        err.throw_error("FP_HMM_FILES")


    region_total_ext = 10000 if not args.region_total_ext else args.region_total_ext
    fp_max_size = 50 if not args.fp_max_size else args.fp_max_size
    fp_min_size = 10 if not args.fp_min_size else args.fp_min_size
    fp_ext = 5 if not args.fp_ext else args.fp_ext
    tc_ext = 100 if not args.tc_ext else args.tc_ext

    # Initializing result set
github tanghaibao / jcvi / jcvi / variation / cnv.py View on Github external
from hmmlearn import hmm

        # Initial population probability
        n = int(10 / step)
        startprob = 1. / n * np.ones(n)
        transmat = mu * np.ones((n, n))
        np.fill_diagonal(transmat, 1 - (n - 1) * mu)

        # The means of each component
        means = np.arange(0, step * n, step)
        means.resize((n, 1, 1))
        # The covariance of each component
        covars = sigma * np.ones((n, 1, 1))

        # Build an HMM instance and set parameters
        model = hmm.GaussianHMM(n_components=n, covariance_type="full")

        # Instead of fitting it from the data, we directly set the estimated
        # parameters, the means and covariance of the components
        model.startprob_ = startprob
        model.transmat_ = transmat
        model.means_ = means
        model.covars_ = covars
        return model