为逻辑回归的对数似然优化负对数似然:scipy

时间:2021-07-13 18:08:00

标签: python optimization scipy logistic-regression

我试图通过 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)

0 个答案:

没有答案