# 损失函数,将对数似然函数去负号,求平均损失defcost(X, y, theta):
left = np.multiply(-y, np.log(model(X, theta)))
right = np.multiply(1- y, np.log(1- model(X, theta)))return np.sum(left - right)/(len(X))
# 计算梯度defgradient(X, y, theta):
grad = np.zeros(theta.shape)
error =(model(X, theta)- y).ravel()for j inrange(len(theta.ravel())):#for each parmeter
term = np.multiply(error, X[:,j])
grad[0, j]= np.sum(term)/len(X)return grad