示例#1
0
print "Sigma"
sigma = mgi.sigma
print sigma


print "mu"
mu = mgi.mu
print mu



#prior = ones((1, states_count)) * 0.5
#data = randn(dimensions_count, steps_count)
#transmat = np.ones((states_count, states_count)) / states_count
#mixmat = ones((1, mixture_components_count)) / mixture_components_count
#mu = np.ones((mixture_components_count, 1, dimensions_count, states_count))
#sigma = np.ones((states_count, mixture_components_count,  dimensions_count, dimensions_count))
model = HMM(threshold=1e-4, adjustSigma=1, adjustMu=1, 
	        adjustPrior=1, adjustTransmition=1, 
	        adjustMix=4, covarianceType='full', maxIter=10)
model.fit(data, prior, transmat, mixmat, mu, sigma)
print model.sigma
print model.mu
print model.transmissions
print model.mix
print model.prior

print model.post_prob_mc
print model.post_prob_mcand_mog
示例#2
0
    return width, height, angle


"""
HMM model with K=4 possible states and observations conditional to the chain are normally distributed.
"""

K = 4
seed = 1995
max_iter = 100
log_interval = 10
verbose = 1

clf = HMM(K=K, max_iter=max_iter, verbose=verbose, log_interval=log_interval)
clf = clf.fit(df_train.values)

print("Log-likelihood at convergence on train %.3f" % clf.lc)
print("Log-likelihood on test %.3f" %
      clf._get_lc(df_test.values, clf.predict(df_test.values)))

fig, ax = plt.subplots(nrows=1, ncols=1)
plot_clusters(df_train.values, clf.mu, clf.labels, ax=ax)

widths = np.zeros(K)
heights = np.zeros(K)
angles = np.zeros(K)

for j in range(K):
    widths[j], heights[j], angles[j] = extract_params_ellipse(clf.Sigma[j])
示例#3
0
simulated data
"""
import numpy as np
from hmm import HMM


def get_input_data(N, T, V):
    return np.random.randint(0, V, size=(N, T))


def get_state(H):
    return np.array(range(0, H))


# simulation
V = [0, 1, 2, 3, 4]
X = get_input_data(20, 10, len(V))
Y = get_state(3)

# training
model = HMM()
model.fit(X, Y, V)

t = model.Transition
e = model.Emission
p = model.Pi

# decoding
model = HMM(t, e, p)
res = model.decode(np.random.randint(0, len(V), size=(2, 10)))
print(res)
示例#4
0
feature_maker = Feature_Maker()

if model_type == "HMM":
    # fit hidden markov model model
    # -------------------------------------------------------------------------
    if load_entities is True:
        data_train = [[(t[0], t[2]) for t in sent] for sent in data_train]
        data_test = [[(t[0], t[2]) for t in sent] for sent in data_test]
    else:
        data_train = [[(t[0], t[1]) for t in sent] for sent in data_train]
        data_test = [[(t[0], t[1]) for t in sent] for sent in data_test]

    hmm = HMM()
    start_time = time.time()
    hmm.fit(data_train)
    print(f"Duration of training: {time.time() - start_time}")

    # evaluation hmm
    # -------------------------------------------------------------------------
    # plot confusion matrix, calculate precision, recall, f1-score
    hmm.evaluate(data_test)
    # show misclassifications
    features_test, labels_test = separate_labels_from_features(data_test)
    predictions = hmm.predict(features_test)
    show_misclassifications(data_test, predictions)

elif model_type == "NB":
    # fit naive bayes model
    # -------------------------------------------------------------------------
    nb = Naive_Bayes()
示例#5
0
文件: test_hmm.py 项目: dsuess/stuff
#!/usr/bin/env python
# encoding: utf-8

from __future__ import division, print_function
import numpy as np
from hmm import _stoch_map, HMM

n_hid = 4
n_obs = 3
steps = 10

hmm_ref = HMM(n_obs, n_hid)
hmm_test = HMM(n_obs, n_hid)

obs = hmm_ref.get_sequence(steps)
hmm_test.fit(obs)

norm = lambda x: np.max(np.abs(x))
print(norm(hmm_ref._mkh - hmm_test._mkh))
print(norm(hmm_ref._mko - hmm_test._mko))
print(norm(hmm_ref._init - hmm_test._init))