Logistic回归:成本函数没有减少 [英] Logistic regression: the cost function is not decreasing

查看:15
本文介绍了Logistic回归:成本函数没有减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上一门关于Coursera的Andrew Ng课程,我尝试在数据集上使用我所学到的关于Logistic回归的知识。但我不能使成本函数减小。

我尝试了不同的学习速率(0.0001、0.003、0.0001…)和迭代次数。可能是我写错了函数,但找不到错误

import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data[:,:2]
Y = (iris.target != 0)*1
m = Y.size
th = np.random.rand(1,3)#theta
xo = np.ones((m,1))
Xi = np.concatenate((xo,X),axis=1)#X intercept
sigma = lambda z: 1/(1+(np.e**-z))
cost = lambda h,y: (np.sum(-y.T*np.log(h)-(1-y).T*np.log(1-h)))/m
grad = lambda h,y,x : np.sum(x.T@(h-y))/m
ite = 100000
lr = 0.0015
for i in range(ite):
    z = Xi@th.T
    th = th- lr*grad(sigma(z),Y,Xi)
    print(cost(sigma(z),Y))

推荐答案

已修复,我不知道为什么在渐变之前写np.sum。但现在起作用了

grad = lambda h,y,x : x.T@(h-y))/m

这篇关于Logistic回归:成本函数没有减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆