File: efficiency.py

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (31 lines) | stat: -rw-r--r-- 760 bytes parent folder | download | duplicates (3)
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
"""
see: https://www.youtube.com/watch?v=umTbivyJoiI
"""
import sys
from math import log2
from collections import Counter

from bitarray.util import huffman_code


def efficiency(freq, code):
    total = sum(freq.values())  # total frequency
    H = 0.0                     # entropy
    L = 0.0                     # average length
    for s in freq.keys():
        p = freq[s] / total     # probability
        H -= p * log2(p)
        L += p * len(code[s])
    print('H =', H)
    print('L =', L)
    return H / L                # efficiency

if len(sys.argv) > 1:
    with open(sys.argv[1], 'rb') as fi:
        plain = fi.read()
else:
    plain = b'aaabbbcde'

freq = Counter(plain)
code = huffman_code(freq)
print("Efficiency =", efficiency(freq, code))