我试图通过 scipy 使用牛顿法最小化负对数似然。但是,我收到了一个 RuntimeError:在 50 次迭代后都未能收敛
from sklearn.datasets import load_iris
from scipy import optimize
import numpy as np
X, y = load_iris(return_X_y=True)
zero_ndx = np.where(y == 0)
ndx = np.append(zero_ndx, np.where(y == 1))
X = X[ndx,:]
y = y[ndx]
y[zero_ndx] = -1
y = y.reshape(-1, 1)
def nll(w,x,y):
print(w)
return sum(np.log(1+np.exp(-y*x@w)))
initial_weights = np.random.normal(size = 4)
optimize.newton(nll, initial_weights,
args = (X, y),
maxiter = 50)