Last active
May 1, 2021 10:41
-
-
Save oniram/1377e20c32471f56e6a874bff319d6a0 to your computer and use it in GitHub Desktop.
check pomegranate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pomegranate import * | |
from pomegranate.parallel import log_probability | |
from pomegranate.io import SequenceGenerator | |
from pomegranate.kmeans import Kmeans | |
from pomegranate.distributions.IndependentComponentsDistribution import IndependentComponentsDistribution | |
from pomegranate.kmeans import Kmeans | |
from pomegranate.io import SequenceGenerator | |
import numpy | |
#get code from pomegranate lib | |
def get_group_with_kmeans(X,n_components): | |
X_, labels_ = [], [] | |
data_generator = SequenceGenerator(X) | |
initialization_batch_size = len(data_generator) | |
data = data_generator.batches() | |
for i in range(initialization_batch_size): | |
batch = next(data) | |
X_.extend(batch[0]) | |
X_concat = numpy.concatenate(X_) | |
if X_concat.ndim == 1: | |
X_concat = X_concat.reshape(X_concat.shape[0], 1) | |
n, d = X_concat.shape | |
clf = Kmeans(n_components) | |
clf.fit(X_concat) | |
y = clf.predict(X_concat) | |
groups = [] | |
for i in range(n_components): | |
groups.append(X_concat[y == i]) | |
return groups | |
def main(): | |
X = numpy.array([ | |
[43.50],[11.73],[11.78],[11.51],[11.37], | |
[22.10],[21.73],[21.78],[21.51],[21.37], | |
[32.10],[31.73],[31.78],[31.51],[31.37], | |
[42.10],[41.73],[41.78],[41.51],[41.37] | |
]) | |
n_components = 2 | |
number_groups = 4 | |
groups = get_group_with_kmeans(X, number_groups) | |
mixtures = [] | |
for i in range(len(groups)): | |
mixtures.append(NormalDistribution(numpy.mean(groups[i]), numpy.std(groups[i]), True)) | |
#Define 2 mixtures for each group | |
dists = [] | |
for i in range(n_components): | |
dists.append(GeneralMixtureModel([mixtures[len(dists)*2],mixtures[len(dists)*2+1]])) | |
starts = numpy.ones(n_components, dtype='float32') / n_components | |
transitions = numpy.ones((n_components, n_components), dtype='float32') / n_components | |
model = HiddenMarkovModel.from_matrix(transitions, dists, starts) | |
model.fit(X) | |
observation=[43.50] | |
trans, ems = model.forward_backward(observation) | |
#sum row from emission table shoul be equal 1 -- OK | |
print(numpy.exp(ems[0])) | |
#Calculate sum probabilities for each state, shoul be equals 1 - NOt OK | |
log_probs = [] | |
for j in range(len(model.states)): | |
if(model.states[j].distribution != None): #skip silent states | |
for m in range(len(model.states[j].distribution.distributions)): | |
weight = numpy.exp(model.states[j].distribution.weights[m]) | |
log_prob = model.states[j].distribution.distributions[m].log_probability(observation) | |
log_probs.append(numpy.exp(log_prob) * weight) | |
print(sum(log_probs)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment