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 cycle for)


  • log2(symbol_dict[symbol] / total) gives the same error
    But I don't see any problem with log2, and it looks legit for me.










share|improve this question




















  • 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












  • @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















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 cycle for)


  • log2(symbol_dict[symbol] / total) gives the same error
    But I don't see any problem with log2, and it looks legit for me.










share|improve this question




















  • 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












  • @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













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 cycle for)


  • log2(symbol_dict[symbol] / total) gives the same error
    But I don't see any problem with log2, and it looks legit for me.










share|improve this question















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 cycle for)


  • 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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












  • @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








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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly