wo kommt der Fehler her? Kann ich eigentlich auch den range so setzen, dass ich im Bereich 0 bis 1 in 0.001-er Schritte gehe?
Traceback (most recent call last):
File "calculate.py", line 11, in <module>
s2 = (1-p) * math.log( (1-p), 2 )
ValueError: math domain error
Code: Alles auswählen
#!/usr/bin/python3
import math
print("Entropie für Wahrscheinlichkeitsbereich berechnen")
list_of_results = []
for p in range( 1, 1000, 1):
# H = -( p * log_2 p + ( 1 - p ) * log_2( 1 - p ) )
s1 = p * math.log(p,2)
s2 = (1-p) * math.log( (1-p), 2 )
cur_res = -1 * ( s1 + s2 )
print("Key-Val: ", str(p), str(cur_res/1000))
cur_res_pair = [ str(p), str(cur_res) ]
list_of_results.append( cur_res_pair )