← Lesson
Entropy
Joeven
Run
Reset
Python loads on first run
import math def entropy(ps): h = 0.0 for p in ps: if p > 0: h -= p * math.log(p, 2) return h def surprise(p): return -math.log(p, 2) peaked = [0.90, 0.05, 0.05] flat = [1 / 3, 1 / 3, 1 / 3] two = [0.5, 0.5] print("surprise of p=0.5 bits", round(surprise(0.5), 3)) print("surprise of p=0.9 bits", round(surprise(0.9), 3)) print("H peaked", round(entropy(peaked), 3)) print("H flat ", round(entropy(flat), 3)) print("H coin ", round(entropy(two), 3)) print("max for 8 labels (uniform)", round(math.log(8, 2), 3)) tools = ["search", "sql", "finish"] print("confident policy H", round(entropy([0.85, 0.10, 0.05]), 3), tools) print("confused policy H", round(entropy([0.34, 0.33, 0.33]), 3), tools)
Run to execute this in your browser. Nothing is sent to a server.