hinge_loss#
- sklearn.metrics.hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None)[source]#
平均铰链损失(非正则化)。
在二分类情况下,假设 y_true 中的标签编码为 +1 和 -1,当预测错误发生时,
margin = y_true * pred_decision总是负值(因为符号不一致),这意味着1 - margin总是大于 1。因此,累积的合页损失是分类器所犯错误的次数的上限。在多分类情况下,函数要求所有标签要么包含在 y_true 中,要么提供一个可选的 labels 参数,其中包含所有标签。多标签合页损失根据 Crammer-Singer 的方法计算。与二分类情况一样,累积的合页损失是分类器所犯错误的次数的上限。
在 用户指南 中阅读更多内容。
- 参数:
- y_true形状为 (n_samples,) 的 array-like
真实目标,由两个值的整数组成。正标签必须大于负标签。
- pred_decision形状为 (n_samples,) 或 (n_samples, n_classes) 的类似数组对象
预测决策,由 decision_function 输出(浮点数)。
- labels类似数组对象, default=None
包含问题的所有标签。用于多分类合页损失。
- sample_weightshape 为 (n_samples,) 的 array-like, default=None
样本权重。
- 返回:
- loss浮点数
平均合页损失。
References
[1][2]Koby Crammer, Yoram Singer. On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines. Journal of Machine Learning Research 2, (2001), 265-292.
示例
>>> from sklearn import svm >>> from sklearn.metrics import hinge_loss >>> X = [[0], [1]] >>> y = [-1, 1] >>> 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([-1, 1, 1], pred_decision) 0.30
在多分类情况下
>>> import numpy as np >>> X = np.array([[0], [1], [2], [3]]) >>> Y = np.array([0, 1, 2, 3]) >>> labels = np.array([0, 1, 2, 3]) >>> est = svm.LinearSVC() >>> est.fit(X, Y) LinearSVC() >>> pred_decision = est.decision_function([[-1], [2], [3]]) >>> y_true = [0, 2, 3] >>> hinge_loss(y_true, pred_decision, labels=labels) 0.56