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

本文探讨了在逻辑回归中sigmoid函数可能导致的数值溢出问题,并提出了一种优化方案来解决这一问题。通过条件判断避免了exp函数在负数输入时产生的极大值,从而降低了错误率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值