如何解决RuntimeWarning:在Logistic回归中使用梯度下降时在登录时遇到的零除
我不知道如何解决这个问题,我只是一个初学者。下面是代码
def sigmoid(X,theta):
z = np.dot(X,theta[1:]) + theta[0]
y =expit(1/1+np.exp(-z))
return (y)
def costFunction(y,yhat):
cost = -y.dot(np.log(yhat)) - (1-y).dot(np.log(1-yhat))
return cost
def gradientFun(X,y,theta,learning_rate,iterations):
cost = []
for i in range(iterations):
yhat = sigmoid(X,theta)
error = yhat - y
grad = X.T.dot(error)
theta[0] = theta[0] - learning_rate * error.sum()
theta[1:] = theta[1:] - learning_rate * grad
cost.append(costFunction(y,yhat))
return cost
m,n = X.shape
theta = np.zeros(1+n)
learning_rate = 0.001
iteration = 1000
cost = gradientFun(X,iteration)
运行代码后出现错误
<ipython-input-87-26e2d5e7bf1c>:2: RuntimeWarning: divide by zero encountered in log
cost = -y.dot(np.log(yhat)) - (1-y).dot(np.log(1-yhat))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。