Ejemplo n.º 1
0
                                    pixel_width=3000, pixel_height=1000,
                                    spread=True, color_key=color_key)
html = file_html(single_plot, CDN, "pomegranate hmm with 3 components")
Html_file.write(html)
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')

T = np.exp(hmm.dense_transition_matrix())[:9, :9]
plt.imshow(T, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
for i, j in itertools.product(range(T.shape[0]), range(T.shape[1])):
        plt.text(j, i, int(T[i, j]*100),
                 horizontalalignment="center",
                 color="white" if T[i, j] > 0.5 else "black")
plt.title('log_likelihood:%0.3f \n state order:' + str(color_key) % hmm.log_probability(X))
plt.savefig('hmm_pomegranate_files/hmm10_pomegranate_transition.png')
data_uri = open(
    'hmm_pomegranate_files/hmm10_pomegranate_transition.png',
    'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
Html_file.write(img_tag)


S = switch_states(T, 0, 1)
S = switch_states(S, 3, 5)
S = switch_states(S, 4, 5)
S = switch_states(S, 5, 6)
plt.figure()
plt.imshow(T, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
Ejemplo n.º 2
0
                                    pixel_width=3000, pixel_height=1000,
                                    spread=True, color_key=color_key)
html = file_html(single_plot, CDN, "pomegranate hmm with 3 components")
Html_file.write(html)
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')
# Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')

T = np.exp(hmm.dense_transition_matrix())[:18, :18]
plt.imshow(T, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
for i, j in itertools.product(range(T.shape[0]), range(T.shape[1])):
        plt.text(j, i, int(T[i, j]*100),
                 horizontalalignment="center",
                 color="white" if T[i, j] > 0.5 else "black")
plt.title('log_likelihood:%0.3f' % hmm.log_probability(X))
plt.savefig('hmm_pomegranate_files/hmm18_pomegranate_transition.png')
data_uri = open(
    'hmm_pomegranate_files/hmm18_pomegranate_transition.png',
    'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
Html_file.write(img_tag)


# # histogram rateCA:
# p = Figure()
# rateCA = np.array(data_thr['rateCA'])
# hist, edges = np.histogram(rateCA, bins=100)
# p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:])
# html = file_html(p, CDN, "hist rateCA")
# Html_file.write(html)
print(transitions)
print("\nThe transition probability from Rainy to Sunny is {:.0f}get_ipython().run_line_magic("".format(100", " * transitions[2, 1]))")


# TODO: input a sequence of 'yes'/'no' values in the list below for testing
observations = ['yes', 'no', 'yes']

assert len(observations) > 0, "You need to choose a sequence of 'yes'/'no' observations to test"

# TODO: use model.forward() to calculate the forward matrix of the observed sequence,
# and then use np.exp() to convert from log-likelihood to likelihood
forward_matrix = np.exp(model.forward(observations))

# TODO: use model.log_probability() to calculate the all-paths likelihood of the
# observed sequence and then use np.exp() to convert log-likelihood to likelihood
probability_percentage = np.exp(model.log_probability(observations))

# Display the forward probabilities
print("         " + "".join(s.name.center(len(s.name)+6) for s in model.states))
for i in range(len(observations) + 1):
    print(" <start> " if i==0 else observations[i - 1].center(9), end="")
    print("".join("{:.0f}get_ipython().run_line_magic("".format(100", " * forward_matrix[i, j]).center(len(s.name) + 6)")
                  for j, s in enumerate(model.states)))

print("\nThe likelihood over all possible paths " + \
      "of this model producing the sequence {} is {:.2f}get_ipython().run_line_magic("\n\n"", "")
      .format(observations, 100 * probability_percentage))


# TODO: input a sequence of 'yes'/'no' values in the list below for testing
observations = ['yes', 'no', 'yes']
Ejemplo n.º 4
0
Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')
Html_file.write(html)
Html_file.write('<br><br><br><br><br><br><br><br><br><br><br><br>')


# transition matrix:
T = np.exp(hmm.dense_transition_matrix())[:3, :3]
plt.imshow(T, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
for i, j in itertools.product(range(T.shape[0]), range(T.shape[1])):
        plt.text(j, i, int(T[i, j]*100),
                 horizontalalignment="center",
                 color=(color_key[i] if i == j
                        else "white" if T[i, j] > 0.5
                        else "black"))
plt.title('Transition probability matrix of the HMM. \n Log likelihood value: %0.3f' % hmm.log_probability(X))
plt.savefig('hmm_pomegranate_files/hmm3_pomegranate_transition.png')
data_uri = open(
    'hmm_pomegranate_files/hmm3_pomegranate_transition.png',
    'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
Html_file.write(img_tag)

# # biclusetering on T:
# biclust = SpectralCoclustering(n_clusters=4)
# biclust.fit(T)
# TT = T[np.argsort(biclust.row_labels_)]
# TT = TT[:, np.argsort(biclust.column_labels_)]
# plt.imshow(T, interpolation='nearest', cmap=plt.cm.Blues)
# plt.colorbar()
# for i, j in itertools.product(range(T.shape[0]), range(T.shape[1])):