训练期间的sklearn逻辑回归损失值

sklearn logistic regression loss value during training

有没有办法在训练逻辑回归时获取每次迭代的损失值?

有一个 SGDRegressor 的工作示例,但不适用于逻辑回归。

我认为你应该更改缩放器的参数,从 **kwargs 到 **params.

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
clf = LogisticRegression(**params)
clf.fit(X_tr, y_tr)
sys.stdout = old_stdout
loss_history = mystdout.getvalue()
loss_list = []
for line in loss_history.split('\n'):
    if(len(line.split("loss: ")) == 1):
        continue
    loss_list.append(float(line.split("loss: ")[-1]))
plt.figure()
plt.plot(np.arange(len(loss_list)), loss_list)
plt.savefig("warmstart_plots/pure_LogRes:"+str(params)+".png")
plt.xlabel("Time in epochs")
plt.ylabel("Loss")
plt.close()