sklearn.metrics.log_loss?
sklearn.metrics.log_loss(y_true, y_pred, *, eps=1e-15, normalize=True, sample_weight=None, labels=None)
對數損失,aka邏輯損失或交叉熵損失。
這是(多項式)邏輯回歸及其擴展(例如神經網絡)中使用的損失函數,定義為邏輯模型的負對數似然性,該邏輯模型為其訓練數據y_true返回y_pred概率。僅為兩個或多個標簽定義對數丟失。對于在{0,1}中具有真實標簽yt且yt = 1的估計概率yp的單個樣本,對數損失為
-log P(yt | yp)=-(yt log(yp)+(1-yt)log(1-yp))
在 用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
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 對于p = 0或p = 1,對數丟失是未定義的,因此概率被限制為max(eps,min(1-eps,p))。 |
normalize | bool, optional (default=True) 如果為true,則返回每個樣本的平均損失。否則,返回每個樣本損失的總和。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
labels | array-like, optional (default=None) 如果未提供,將從y_true推斷標簽。如果label為None且y_pred具有形狀(n_samples,),則假定標簽為二進制,并從y_true推論得出。 版本0.18中的新功能。 |
返回值 | 說明 |
---|---|
loss | float |
注
使用的對數是自然對數(以e為底)。
參考
C.M. Bishop (2006). Pattern Recognition and Machine Learning. Springer, p. 209.
示例
>>> from sklearn.metrics import log_loss
>>> log_loss(["spam", "ham", "ham", "spam"],
... [[.1, .9], [.9, .1], [.8, .2], [.35, .65]])
0.21616...