【问题标题】:While creating cost function in logistic regression getting "MATH DOMAIN ERROR"在逻辑回归中创建成本函数时得到“数学域错误”
【发布时间】:2019-12-29 20:58:07
【问题描述】:

我正在尝试为逻辑回归创建成本函数。将值传递给该函数时,我得到“数学域错误”。

def Cost_Function(X,Y,theta,m):
    sumOfErrors = 0
    for i in range(m):
        xi = X[i]
        hi = Hypothesis(theta,xi)
        if Y[i] == 1:
            error = Y[i] * math.log(hi)
        elif Y[i] == 0:
            error = (1-Y[i]) * math.log(1-hi)
        sumOfErrors += error
    const = -1/m
    J = const * sumOfErrors
    print ('cost is ', J )
    return J```

<ipython-input-14-4cd33d7e280c> in Cost_Function(X, Y, theta, m)
      7             error = Y[i] * math.log(hi)
      8         elif Y[i] == 0:
----> 9             error = (1-Y[i]) * math.log(1-hi)
     10         sumOfErrors += error
     11     const = -1/m

ValueError: math domain error

【问题讨论】:

  • 这意味着1 - hi &lt;= 0

标签: python-3.x numpy machine-learning logistic-regression


【解决方案1】:

这里发生了错误。 error = (1-Y[i]) * math.log(1-hi)

log 操作通常对大于 0 的数执行。如果对小于或等于 0 的数执行log,则结果未定义。结果,你得到了那个例外。

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2015-09-28
    • 2018-03-01
    • 2021-02-16
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多