《机器学习实战》笔记:RuntimeWarning: overflow encountered in exp

>>> logRegres.multiTest()
Warning (from warnings module):
  File "E:/AI/FirstPythonProj\logRegres.py", line 13
    return 1.0/(1+exp(-inX))
RuntimeWarning: overflow encountered in exp
the error rate of this test is: 0.388060
the error rate of this test is: 0.388060
the error rate of this test is: 0.388060
the error rate of this test is: 0.388060
the error rate of this test is: 0.417910
the error rate of this test is: 0.298507
the error rate of this test is: 0.298507
the error rate of this test is: 0.358209
the error rate of this test is: 0.298507
the error rate of this test is: 0.298507

after 10 iterations the average error rate is: 0.352239

再次运行logRegres.multiTest()时,没有第一次的警告,sigmoid函数优化可避免类似问题:

def sigmoid(inX):
    from numpy import exp
    return 1.0/(1+exp(-inX))
def sigmoid(inX):
    from numpy import exp
    #return 1.0/(1+exp(-inX))
    #优化20180412
    if inX>=0:
        return 1.0/(1+exp(-inX))
    else:
        return exp(inX)/(1+exp(inX))


你可能感兴趣的:(《机器学习实战》笔记:RuntimeWarning: overflow encountered in exp)