-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy.py
executable file
·49 lines (32 loc) · 1.03 KB
/
entropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Shannon entropies.
Taken from https://stackoverflow.com/a/2979208/8295404.
Author: Abdul Rahman Dabbour
License: MIT
"""
import math
def shannon_entropy(string: str) -> float:
"""Calculates the Shannon entropy of a string.
Taken from https://stackoverflow.com/a/2979208/8295404.
Args:
string (str): The query string.
Returns:
Shannon entropy of string.
"""
prob = [
float(string.count(c)) / len(string)
for c in dict.fromkeys(list(string))
]
entropy = -sum(p * math.log(p) / math.log(2.0) for p in prob)
return entropy
def ideal_shannon_entropy(length: int) -> float:
"""Calculates the ideal Shannon entropy of a string with given length.
Taken from https://stackoverflow.com/a/2979208/8295404.
Args:
length (int): The query length.
Returns:
Ideal Shannon entropy of string of given length.
"""
prob = 1.0 / length
return -1.0 * length * prob * math.log(prob) / math.log(2.0)