import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
x = np.random.rand(10000)
y = np.random.randn(10000)
#Shannan entropy
H_x= sp.entropy(x,alpha=1)
H_y= sp.entropy(y,alpha=1)
#Rényi entropy
Hr_x= sp.entropy(x,alpha=2)
Hr_y= sp.entropy(y,alpha=2)
H_xy= sp.entropy_joint(x,y)
H_x1y= sp.entropy_cond(x,y)
H_y1x= sp.entropy_cond(y,x)
I_xy = sp.mutual_Info(x,y)
H_xy_cross= sp.entropy_cross(x,y)
D_xy= sp.entropy_kld(x,y)
print('Shannan entropy')
print('Entropy of x: H(x) = ',H_x)
print('Entropy of y: H(y) = ',H_y)
print('-')
print('Rényi entropy')
print('Entropy of x: H(x) = ',Hr_x)
print('Entropy of y: H(y) = ',Hr_y)
print('-')
print('Mutual Information I(x,y) = ',I_xy)
print('Joint Entropy H(x,y) = ',H_xy)
print('Conditional Entropy of : H(x|y) = ',H_x1y)
print('Conditional Entropy of : H(y|x) = ',H_y1x)
print('-')
print('Cross Entropy of : H(x,y) = :',H_xy_cross)
print('Kullback–Leibler divergence : Dkl(x,y) = :',D_xy)
plt.figure(figsize=(12,5))
plt.subplot(121)
sp.HistPlot(x,show=False)
plt.subplot(122)
sp.HistPlot(y,show=False)
plt.show()
Shannan entropy Entropy of x: H(x) = 4.4581180171280685 Entropy of y: H(y) = 5.04102391756942 - Rényi entropy Entropy of x: H(x) = 4.456806796146617 Entropy of y: H(y) = 4.828391418226062 - Mutual Information I(x,y) = 0.05934937774825322 Joint Entropy H(x,y) = 9.439792556949234 Conditional Entropy of : H(x|y) = 4.398768639379814 Conditional Entropy of : H(y|x) = 4.9816745398211655 - Cross Entropy of : H(x,y) = : 11.591688735915701 Kullback–Leibler divergence : Dkl(x,y) = : 4.203058010473213
from spkit.data import load_data
X,ch_names = load_data.eegSample()
print(X.shape)
print(ch_names)
(2048, 14) ['AF3', 'F7', 'F3', 'FC5', 'T7', 'P7', 'O1', 'O2', 'P8', 'T8', 'FC6', 'F4', 'F8', 'AF4']
x1 =X[:,0] #'AF3' - Frontal Lobe
x2 =X[:,6] #'O1' - Occipital Lobe
#Shannan entropy
H_x1= sp.entropy(x1,alpha=1)
H_x2= sp.entropy(x2,alpha=1)
#Rényi entropy
Hr_x1= sp.entropy(x1,alpha=2)
Hr_x2= sp.entropy(x2,alpha=2)
#Joint entropy
H_x12= sp.entropy_joint(x1,x2)
#Conditional Entropy
H_x12= sp.entropy_cond(x1,x2)
H_x21= sp.entropy_cond(x2,x1)
#Mutual Entropy
I_x12 = sp.mutual_Info(x1,x2)
#Cross Entropy
H_x12_cross= sp.entropy_cross(x1,x2)
#Diff Entropy
D_x12= sp.entropy_kld(x1,x2)
print('Shannan entropy')
print('Entropy of x1: H(x1) =\t ',H_x1)
print('Entropy of x2: H(x2) =\t ',H_x2)
print('-')
print('Rényi entropy')
print('Entropy of x1: H(x1) =\t ',Hr_x1)
print('Entropy of x2: H(x2) =\t ',Hr_x2)
print('-')
print('Joint Entropy H(x1,x2) =\t',H_x12)
print('Mutual Information I(x1,x2) =\t',I_x12)
print('Conditional Entropy of : H(x1|x2) =\t',H_x12)
print('Conditional Entropy of : H(x2|x1) =\t',H_x21)
print('-')
print('Cross Entropy of : H(x1,x2) =\t',H_x12_cross)
print('Kullback–Leibler divergence : Dkl(x1,x2) =\t',D_x12)
Shannan entropy Entropy of x1: H(x1) = 4.811416424350645 Entropy of x2: H(x2) = 4.697278111823318 - Rényi entropy Entropy of x1: H(x1) = 4.239955818998481 Entropy of x2: H(x2) = 4.145472461333239 - Joint Entropy H(x1,x2) = 3.655513859907648 Mutual Information I(x1,x2) = 1.1559025644429965 Conditional Entropy of : H(x1|x2) = 3.655513859907648 Conditional Entropy of : H(x2|x1) = 3.541375547380321 - Cross Entropy of : H(x1,x2) = 5.686498222841184 Kullback–Leibler divergence : Dkl(x1,x2) = 0.7202757885313226