log2 ValueError: math domain error while trying to find entropy
up vote
-1
down vote
favorite
So I'm trying to make a fucntion, that will find D (I don't know english word for it) for text with H (entropy). But getting an error with next code
symbol_dict- is a dictonary with alphabet symbols as keys and how much script meets symbol in text, lang_x it's list of needed alphabet symbols (same as keys in dictonary)
def emk_otn_f(symbol_dict, lang_x):
from math import log2
total = 0
entropy = 0
entropy_max = 0
for i in range(len(symbol_dict)): # Here I'm count the sum of all symbols in text
symbol = lang_x[i]
total = total + symbol_dict[symbol]
for i in range(len(symbol_dict)): # Here I'm trying to find an entropy with formula (-Sigma... etc.)
symbol = lang_x[i]
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
entropy = entropy * (-1)
for i in range(len(symbol_dict)): #max Entropy
entropy_max = entropy_max + (1/total * log2(1/total))
entropy_max = entropy_max * (-1)
emk = entropy_max - entropy
emk_otn = (emk/entropy_max) * 100
return emk_otn
But the only thing i'm see after execution is:
File "formula.py", line 11, in emk_otn_f
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
ValueError: math domain error
After googling and testing I finded out that:
(symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)is not working
(symbol_dict[symbol] / total)have legit-like values (I was printing them in cyclefor)
log2(symbol_dict[symbol] / total)gives the same error
But I don't see any problem with log2, and it looks legit for me.
python python-3.x math
add a comment |
up vote
-1
down vote
favorite
So I'm trying to make a fucntion, that will find D (I don't know english word for it) for text with H (entropy). But getting an error with next code
symbol_dict- is a dictonary with alphabet symbols as keys and how much script meets symbol in text, lang_x it's list of needed alphabet symbols (same as keys in dictonary)
def emk_otn_f(symbol_dict, lang_x):
from math import log2
total = 0
entropy = 0
entropy_max = 0
for i in range(len(symbol_dict)): # Here I'm count the sum of all symbols in text
symbol = lang_x[i]
total = total + symbol_dict[symbol]
for i in range(len(symbol_dict)): # Here I'm trying to find an entropy with formula (-Sigma... etc.)
symbol = lang_x[i]
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
entropy = entropy * (-1)
for i in range(len(symbol_dict)): #max Entropy
entropy_max = entropy_max + (1/total * log2(1/total))
entropy_max = entropy_max * (-1)
emk = entropy_max - entropy
emk_otn = (emk/entropy_max) * 100
return emk_otn
But the only thing i'm see after execution is:
File "formula.py", line 11, in emk_otn_f
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
ValueError: math domain error
After googling and testing I finded out that:
(symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)is not working
(symbol_dict[symbol] / total)have legit-like values (I was printing them in cyclefor)
log2(symbol_dict[symbol] / total)gives the same error
But I don't see any problem with log2, and it looks legit for me.
python python-3.x math
1
Did you precede that problem line withprint(symbol_dict[symbol], total)to see ifsymbol_dict[symbol]is zero or negative ortotalis negative? Thelog2function from themathunit handles only positive numbers and gives your error for zero or negative numbers.
– Rory Daulton
Nov 11 at 19:24
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there isNoneI guess problem can be in this one. I will try to changefortowhileand count from-1 to len()
– Pavel Konstantinov
Nov 12 at 3:45
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So I'm trying to make a fucntion, that will find D (I don't know english word for it) for text with H (entropy). But getting an error with next code
symbol_dict- is a dictonary with alphabet symbols as keys and how much script meets symbol in text, lang_x it's list of needed alphabet symbols (same as keys in dictonary)
def emk_otn_f(symbol_dict, lang_x):
from math import log2
total = 0
entropy = 0
entropy_max = 0
for i in range(len(symbol_dict)): # Here I'm count the sum of all symbols in text
symbol = lang_x[i]
total = total + symbol_dict[symbol]
for i in range(len(symbol_dict)): # Here I'm trying to find an entropy with formula (-Sigma... etc.)
symbol = lang_x[i]
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
entropy = entropy * (-1)
for i in range(len(symbol_dict)): #max Entropy
entropy_max = entropy_max + (1/total * log2(1/total))
entropy_max = entropy_max * (-1)
emk = entropy_max - entropy
emk_otn = (emk/entropy_max) * 100
return emk_otn
But the only thing i'm see after execution is:
File "formula.py", line 11, in emk_otn_f
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
ValueError: math domain error
After googling and testing I finded out that:
(symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)is not working
(symbol_dict[symbol] / total)have legit-like values (I was printing them in cyclefor)
log2(symbol_dict[symbol] / total)gives the same error
But I don't see any problem with log2, and it looks legit for me.
python python-3.x math
So I'm trying to make a fucntion, that will find D (I don't know english word for it) for text with H (entropy). But getting an error with next code
symbol_dict- is a dictonary with alphabet symbols as keys and how much script meets symbol in text, lang_x it's list of needed alphabet symbols (same as keys in dictonary)
def emk_otn_f(symbol_dict, lang_x):
from math import log2
total = 0
entropy = 0
entropy_max = 0
for i in range(len(symbol_dict)): # Here I'm count the sum of all symbols in text
symbol = lang_x[i]
total = total + symbol_dict[symbol]
for i in range(len(symbol_dict)): # Here I'm trying to find an entropy with formula (-Sigma... etc.)
symbol = lang_x[i]
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
entropy = entropy * (-1)
for i in range(len(symbol_dict)): #max Entropy
entropy_max = entropy_max + (1/total * log2(1/total))
entropy_max = entropy_max * (-1)
emk = entropy_max - entropy
emk_otn = (emk/entropy_max) * 100
return emk_otn
But the only thing i'm see after execution is:
File "formula.py", line 11, in emk_otn_f
entropy = entropy + (symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)
ValueError: math domain error
After googling and testing I finded out that:
(symbol_dict[symbol] / total) * log2(symbol_dict[symbol] / total)is not working
(symbol_dict[symbol] / total)have legit-like values (I was printing them in cyclefor)
log2(symbol_dict[symbol] / total)gives the same error
But I don't see any problem with log2, and it looks legit for me.
python python-3.x math
python python-3.x math
edited Nov 11 at 19:23
martineau
65.1k987176
65.1k987176
asked Nov 11 at 19:17
Pavel Konstantinov
62
62
1
Did you precede that problem line withprint(symbol_dict[symbol], total)to see ifsymbol_dict[symbol]is zero or negative ortotalis negative? Thelog2function from themathunit handles only positive numbers and gives your error for zero or negative numbers.
– Rory Daulton
Nov 11 at 19:24
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there isNoneI guess problem can be in this one. I will try to changefortowhileand count from-1 to len()
– Pavel Konstantinov
Nov 12 at 3:45
add a comment |
1
Did you precede that problem line withprint(symbol_dict[symbol], total)to see ifsymbol_dict[symbol]is zero or negative ortotalis negative? Thelog2function from themathunit handles only positive numbers and gives your error for zero or negative numbers.
– Rory Daulton
Nov 11 at 19:24
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there isNoneI guess problem can be in this one. I will try to changefortowhileand count from-1 to len()
– Pavel Konstantinov
Nov 12 at 3:45
1
1
Did you precede that problem line with
print(symbol_dict[symbol], total) to see if symbol_dict[symbol] is zero or negative or total is negative? The log2 function from the math unit handles only positive numbers and gives your error for zero or negative numbers.– Rory Daulton
Nov 11 at 19:24
Did you precede that problem line with
print(symbol_dict[symbol], total) to see if symbol_dict[symbol] is zero or negative or total is negative? The log2 function from the math unit handles only positive numbers and gives your error for zero or negative numbers.– Rory Daulton
Nov 11 at 19:24
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there is
None I guess problem can be in this one. I will try to change for to while and count from -1 to len()– Pavel Konstantinov
Nov 12 at 3:45
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there is
None I guess problem can be in this one. I will try to change for to while and count from -1 to len()– Pavel Konstantinov
Nov 12 at 3:45
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252281%2flog2-valueerror-math-domain-error-while-trying-to-find-entropy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Did you precede that problem line with
print(symbol_dict[symbol], total)to see ifsymbol_dict[symbol]is zero or negative ortotalis negative? Thelog2function from themathunit handles only positive numbers and gives your error for zero or negative numbers.– Rory Daulton
Nov 11 at 19:24
@RoryDaulton I checked it now, but it seems they both positive. But at the end of output (after executing it for some text) there is
NoneI guess problem can be in this one. I will try to changefortowhileand count from-1 to len()– Pavel Konstantinov
Nov 12 at 3:45