【发布时间】: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 <= 0
标签: python-3.x numpy machine-learning logistic-regression