在python中记录基数2

时间:2013-11-18 04:36:36

标签: python-2.7

我正在尝试计算ID树中的平均症状。我的代码如下:

Republican_yes = yes.count('Republican')
Democrat_yes = yes.count('Democrat')
Republican_no = no.count('Republican')
Democrat_no = no.count('Democrat')
Indep_yes = yes.count('Independent')
Indep_no = no.count('Independent')

disorder_yes= Republican_yes/len(yes)*(math.log(float(Republican_yes)/len(yes),2))+   Democrat_yes/len(yes)*(math.log(float(Democrat_yes)/len(yes),2))+Indep_yes/len(yes)*(math.log(float(Indep_yes)/len(yes),2))

disorder_no= Republican_no/len(no)*(math.log(float(Republican_no)/len(no),2))+Democrat_no/len(no)*(math.log(float(Democrat_no)/len(no),2))+Indep_no/len(no)*(math.log(float(Indep_no)/len(no),2))

avgdisorder = -len(yes)/(len(yes)+len(no))*disorder_yes - len(no)/(len(yes)+len(no))*disorder_no

返回avgdisorder

为什么我一直收到数学域错误?

1 个答案:

答案 0 :(得分:1)

检查长度是否为0,否则获得MathError

if len(yes):
    disorder_yes= Republican_yes/len(yes)*(math.log(float(Republican_yes)/len(yes),2))+   Democrat_yes/len(yes)*(math.log(float(Democrat_yes)/len(yes),2))+Indep_yes/len(yes)*(math.log(float(Indep_yes)/len(yes),2))

if len(no):
    disorder_no= Republican_no/len(no)*(math.log(float(Republican_no)/len(no),2))+Democrat_no/len(no)*(math.log(float(Democrat_no)/len(no),2))+Indep_no/len(no)*(math.log(float(Indep_no)/len(no),2))

if len(yes) or len(no):
    avgdisorder = -len(yes)/(len(yes)+len(no))*disorder_yes - len(no)/(len(yes)+len(no))*disorder_no

如果需要,您可以随时根据您的要求为所有3个else语句添加if子句。