maxframe.learn.metrics.log_loss#

maxframe.learn.metrics.log_loss(y_true, y_pred, *, eps=1e-15, normalize=True, sample_weight=None, labels=None, execute=False, session=None, run_kwargs=None)[源代码]#

对数损失,也称为逻辑损失或交叉熵损失。

这是(多项式)逻辑回归及其扩展(如神经网络)中使用的损失函数,定义为返回 y_pred 概率的逻辑模型对其训练数据 y_true 的负对数似然。对数损失仅对两个或更多标签有定义。对于一个真实标签为 \(y \in \{0,1\}\) 且概率估计为 \(p = \operatorname{Pr}(y = 1)\) 的样本,对数损失为:

\[L_{\log}(y, p) = -(y \log (p) + (1 - y) \log (1 - p))\]

更多内容请参见 用户指南

参数:
  • y_true (array-like or label indicator matrix) -- n_samples 个样本的真实(正确)标签。

  • y_pred (array-like of float, shape = (n_samples, n_classes) or (n_samples,)) -- 由分类器的 predict_proba 方法返回的预测概率。如果 y_pred.shape = (n_samples,),则假定所提供的概率为正类的概率。y_pred 中的标签被假定为按字母顺序排列,与 preprocessing.LabelBinarizer 的处理方式一致。

  • eps (float, default=1e-15) -- 当 p=0 或 p=1 时,对数损失未定义,因此概率被截断为 max(eps, min(1 - eps, p))。

  • normalize (bool, default=True) -- 如果为 true,则返回每个样本的平均损失。否则,返回所有样本损失的总和。

  • sample_weight (array-like of shape (n_samples,), default=None) -- 样本权重。

  • labels (array-like, default=None) -- 如果未提供,则会从 y_true 推断标签。如果 labelsNoney_pred 的形状为 (n_samples,),则假定标签为二分类,并从 y_true 推断。

返回:

损失

返回类型:

float

备注

所使用的对数是自然对数(以 e 为底)。

示例

>>> from maxframe.learn.metrics import log_loss
>>> log_loss(["spam", "ham", "ham", "spam"],
...          [[.1, .9], [.9, .1], [.8, .2], [.35, .65]])
0.21616...

引用

C.M. Bishop (2006). Pattern Recognition and Machine Learning. Springer, p. 209.