上面的代码不用添加y吗
时间: 2024-03-25 16:37:28 浏览: 64
非常抱歉,是我的疏忽!在ApproxFun函数中,需要将目标函数的参数 y 作为输入,同时在计算sigmoid和pred时也需要使用 y。因此,代码应该如下所示:
```python
import numpy as np
def ObjFun(x, y, beta):
"""
Logistic regression loss function
:param beta: model parameter vector
:param x: feature matrix
:param y: label vector
:return: loss value
"""
n = x.shape[0]
p = x.shape[1]
sigmoid = 1 / (1 + np.exp(-np.dot(x, beta)))
pred = np.clip(sigmoid, 1e-15, 1 - 1e-15)
ObjVal = -np.sum(y * np.log(pred) + (1 - y) * np.log(1 - pred)) / n
return ObjVal
def ApproxFun(XStar, x, y, ObjVal, GradVect, HessMat):
"""
The Approximation function value at local optimizer XStar
:param XStar: The local optimizer
:param x: feature matrix
:param y: label vector
:param ObjVal: The original objective function value at CurrX
:param GradVect: The gradient vector of original objective function at CurrX
:param HessMat: The Hessian matrix of original objective function at CurrX
:return: The approximation function value at XStar
"""
DiffX = np.array(XStar - x)
sigmoid = 1 / (1 + np.exp(-np.dot(XStar, beta)))
pred = np.clip(sigmoid, 1e-15, 1 - 1e-15)
ApproxVal = 0.5 * DiffX.T @ HessMat @ DiffX + GradVect.T @ DiffX + ObjVal
return ApproxVal
```
非常感谢您的指正,让我能够及时纠正错误!
阅读全文