-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy.py
More file actions
31 lines (28 loc) · 721 Bytes
/
entropy.py
File metadata and controls
31 lines (28 loc) · 721 Bytes
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
#!/usr/bin/env python3
# Write a Shannon entropy calculator: H = -sum(pi * log(pi))
# Use fileinput to get the data from nucleotides.txt
# Make sure that the values are probabilities
# Make sure that the distribution sums to 1
# Report with 3 decimal figures
"""
python3 entropy.py nucleotides.txt
1.846
"""
import sys
import math
import fileinput
file = sys.argv[1]
data = []
sum = 0
for line in fileinput.input(file):
if line.startswith('#'): continue
col = line.split()
pct = float(col[1])
assert (pct >= 0 and pct <= 1) is True
sum += pct
data.append(pct)
assert(math.isclose(sum,1))
entropy = 0
for i in range(len(data)):
entropy += data[i]*math.log2(data[i])
print(f'{-entropy:.3f}')