From d3c98442a970a572dbc8d90279018396f0873df3 Mon Sep 17 00:00:00 2001 From: Conrad Date: Mon, 5 Oct 2020 09:11:13 +0200 Subject: [PATCH] Added max entropy calculator --- entro_py_min/entro_py_min.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/entro_py_min/entro_py_min.py b/entro_py_min/entro_py_min.py index d03cf53..cb42844 100644 --- a/entro_py_min/entro_py_min.py +++ b/entro_py_min/entro_py_min.py @@ -4,38 +4,36 @@ import math # Calculates the entropy of a given string # Returns the entropy and an alphabet with the calculated probabilities def calculateEntropy(input_string): - alphabet, alphabet_size, entropy = {}, 0, 0 + alphabet, alphabet_size, entropy = {}, len(input_string), 0 for char in input_string: if char in alphabet: alphabet[char] += 1 else: alphabet[char] = 1 - alphabet_size += 1 for char in alphabet: alphabet[char] = alphabet[char] / alphabet_size entropy -= alphabet[char] * math.log(alphabet[char], 2) - return entropy, alphabet + max_entropy = - len(alphabet) * (1 / len(alphabet) * math.log(1 / len(alphabet), 2)) + return entropy, alphabet, max_entropy # Calculates the entropy of a given string # Returns only the entropy in bits as this is the minimal function def calculateEntropyMin(input_string): - alphabet, alphabet_size, entropy = {}, 0, 0 + alphabet, alphabet_size, entropy = {}, len(input_string), 0 for char in input_string: if char in alphabet: alphabet[char] += 1 else: alphabet[char] = 1 - alphabet_size += 1 for char in alphabet: i = alphabet[char] / alphabet_size entropy -= i * math.log(i, 2) - return entropy