Note
Go to the end to download the full example code or to run this example in your browser via JupyterLite or Binder
Entropy - Discreet Source¶
An example of computation of entropy for a discreet source.

Random variable x :
[4. 0. 5. 4. 4. 3. 2. 6. 3. 3. 6. 5. 1. 5. 2. 8. 9. 5. 8. 1. 5. 1. 4. 1.
1. 6. 2. 1. 2. 3.]
import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
# Generate a discreet random variable x
np.random.seed(2)
x = (np.random.rand(30)*10).round()
# compute entropy
Hx = sp.entropy(x,is_discrete=True)
print('Random variable x : \n',x)
x_u,frq = np.unique(x, return_counts=True)
prob = frq/frq.sum()
plt.bar(x_u,prob)
plt.xlabel('x')
plt.ylabel('p(x)')
plt.title(f'Entropy H(x) = {Hx.round(3)} bits')
plt.show()
Total running time of the script: (0 minutes 0.050 seconds)
Related examples