【发布时间】:2019-07-20 05:50:24
【问题描述】:
我目前正在 Coursera 上学习 Andrew Ng 课程,我尝试将我学到的关于逻辑回归的知识用于数据集。但我不能让成本函数减少。
我尝试了不同的学习率(0.001、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))
【问题讨论】:
-
无法重现:
NameError: name 'Y' is not defined -
欢迎来到 SO;请查看如何创建minimal reproducible example
-
@desertnaut 对不起,我是 stackoverflow 的新手,我编辑了它,所以现在可以重现
标签: python machine-learning jupyter-notebook logistic-regression