Entropy - Discreet Source

An example of computation of entropy for a discreet source.

Entropy H(x) = 2.978 bits
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

Entropy - Real-Valued Source

Entropy - Real-Valued Source

Sample and Approximate Entropy: Comparison

Sample and Approximate Entropy: Comparison

Sinusoidal Model: Analysis and Synthesis

Sinusoidal Model: Analysis and Synthesis

Ramanujan Filter Banks Example

Ramanujan Filter Banks Example

Naive Bayes classifier - Visualisation

Naive Bayes classifier - Visualisation

Gallery generated by Sphinx-Gallery