sklearn.metrics.hinge_loss?

sklearn.metrics.hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None)

平均鉸鏈損耗(非常規)

在二元類情況下,假設y_true中的標簽用+1和-1編碼,則在發生預測錯誤時,margin = y_true * pred_decision始終為負(因為符號不同),這意味著1-margin始終大于1。 因此,累積的鉸鏈損耗是分類器所犯錯誤數量的上限。

在多類情況下,該函數期望所有標簽都包含在y_true中,或者提供一個包含所有標簽的可選標簽參數。 多標簽邊距是根據Crammer-Singer的方法計算的。 與二進制情況一樣,累積的鉸鏈損耗是分類器得出錯誤數量的上限。

用戶指南中閱讀更多內容。

參數 說明
y_true array, shape = [n_samples]
真實目標,由兩個值的整數組成。 正標簽必須大于負標簽。
pred_decision array, shape = [n_samples] or [n_samples, n_classes]
預測決策,由Decision_function輸出(浮點數)。
labels array, optional, default None
包含問題的所有標簽。 用于多類鉸鏈損耗。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。
返回值 說明
loss float

參考

1 Wikipedia entry on the Hinge loss

2 Koby Crammer, Yoram Singer. On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines. Journal of Machine Learning Research 2, (2001), 265-292

3 L1 AND L2 Regularization for Multiclass Hinge Loss Models by Robert C. Moore, John DeNero.

示例

>>> from sklearn import svm
>>> from sklearn.metrics import hinge_loss
>>> X = [[0], [1]]
>>> y = [-11]
>>> est = svm.LinearSVC(random_state=0)
>>> est.fit(X, y)
LinearSVC(random_state=0)
>>> pred_decision = est.decision_function([[-2], [3], [0.5]])
>>> pred_decision
array([-2.18...,  2.36...,  0.09...])
>>> hinge_loss([-111], pred_decision)
0.30...

在多類情況下:

>>> import numpy as np
>>> X = np.array([[0], [1], [2], [3]])
>>> Y = np.array([0123])
>>> labels = np.array([0123])
>>> est = svm.LinearSVC()
>>> est.fit(X, Y)
LinearSVC()
>>> pred_decision = est.decision_function([[-1], [2], [3]])
>>> y_true = [023]
>>> hinge_loss(y_true, pred_decision, labels=labels)
0.56...