Skip to content

Instantly share code, notes, and snippets.

@kentatogashi
Last active January 11, 2018 02:41
Show Gist options
  • Save kentatogashi/6804900859009010fbbf5de4b7e7f331 to your computer and use it in GitHub Desktop.
Save kentatogashi/6804900859009010fbbf5de4b7e7f331 to your computer and use it in GitHub Desktop.
entropy 計算
import numpy as np
import sys
def _str2ep(s):
done = set()
l = len(s)
arr = []
for i in s:
if not i in done:
x = s.count(i)
arr.append(x/l)
done.add(i)
return arr
def entropy(s):
arr = _str2ep(s)
return -(arr * np.ma.log(arr)).sum()
s = sys.argv[1]
print(entropy(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment