Last active
January 11, 2018 02:41
-
-
Save kentatogashi/6804900859009010fbbf5de4b7e7f331 to your computer and use it in GitHub Desktop.
entropy 計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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