Skip to content
Permalink
Browse files

Updated.

  • Loading branch information
emeryberger committed Jan 2, 2020
1 parent 7e7312c commit 90836593914e79698522ddaaf4c684e384fde4d5
Showing with 26 additions and 1 deletion.
  1. +26 −1 experimental/infogain.py
@@ -67,6 +67,23 @@ def entropy(counts):
entropy += -freq * math.log2(freq)
return entropy

"""Compute average Shannon entropy with one less item."""
@staticmethod
def average_reduced_normalized_entropy(counts):
total_entropy = 0
total = sum(counts) - 1
for j in range(0, len(counts)):
entropy = 0
for i in range(0,len(counts)):
if i == j:
freq = (counts[i] - 1) / total
else:
freq = counts[i] / total
if freq != 0:
entropy += -freq * math.log2(freq)
total_entropy += entropy
return (total_entropy / len(counts)) / math.log2(total)

"""Compute the salience (entropy-discounted "importance", from 0 to 1) of an index in a pmf."""
@staticmethod
def salience(counts, index):
@@ -318,4 +335,12 @@ def bitor(x,y):
for i in range(0, len(z)):
for j in range(0, len(z[0])):
pass


arr = [82, 9, 6, 3]
print("normalized entropy = " + str(InfoGain.normalized_entropy(arr)))
print("normalized entropy [82, 9, 6] = " + str(InfoGain.normalized_entropy([82, 9, 6])))
print("normalized entropy [82, 9, 3] = " + str(InfoGain.normalized_entropy([82, 9, 3])))
print("normalized entropy [82, 6, 3] = " + str(InfoGain.normalized_entropy([82, 6, 3])))
print("normalized entropy [9, 6, 3] = " + str(InfoGain.normalized_entropy([9, 6, 3])))
print("average reduced entropy = " + str(InfoGain.average_reduced_normalized_entropy(arr)))
print("salience try = " + str((1 - 82/100) * (InfoGain.average_reduced_normalized_entropy(arr)/InfoGain.normalized_entropy(arr))))

0 comments on commit 9083659

Please sign in to comment.
You can’t perform that action at this time.