Skip to content

Instantly share code, notes, and snippets.

@nbortolotti
Last active November 28, 2018 12:59
Show Gist options
  • Save nbortolotti/fe662b7ff7a79ee3a268ec98a629de41 to your computer and use it in GitHub Desktop.
Save nbortolotti/fe662b7ff7a79ee3a268ec98a629de41 to your computer and use it in GitHub Desktop.
Tensorflow Probability, what is the probability that of 6 randomly selected patients, 4 will recover? article support
#tensorflow and tensorflow_probability imports
import tensorflow as tf
import tensorflow_probability as tfp
#matplotlib import for visualizations
import matplotlib.pyplot as plt;
# numpy to support transformation and visualiation
import numpy as np;
# I'm going to use eager mode and a shortcut to distributions
tfe = tf.contrib.eager
tfd = tfp.distributions
sess = tf.InteractiveSession()
# Model
n = 6.
count = tf.range(
start=0.,
limit= n +1)
p = tf.constant([0.25])
dist = tfd.Binomial(total_count=n, probs=p).prob(count)
# dist.eval() support elements if is needed
# Support function to convert tensor to np
def tensorConvertor(tensors):
if tf.executing_eagerly():
return tf.contrib.framework.nest.pack_sequence_as(
tensors,
[t.numpy() if tf.contrib.framework.is_tensor(t) else t
for t in tf.contrib.framework.nest.flatten(tensors)])
return sess.run(tensors)
dist_np= tensorConvertor(dist) #Support function
# Visualization
for i, v in enumerate(dist_np):
objects = (0, 1, 2, 3, 4, 5, 6)
y_pos = np.arange(len(objects))
plt.barh(y_pos, dist_np, align='center', alpha=0.5)
plt.ylabel('Patients')
plt.xlabel('Probability')
plt.title('Recorver probability patients Hospital')
plt.text(v, i , str(v), color='green', fontweight='bold')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment