hinge loss 代码示例
程序员文章站
2022-06-26 13:37:50
...
from sklearn import svm
from sklearn.metrics import hinge_loss
X = [[0], [1]]
y = [-1, 1]
model = svm.LinearSVC(random_state=0)
model.fit(X, y)
pred_decision = model.decision_function([[-2], [3], [0.5]])
print(pred_decision)
print(hinge_loss([-1, 1, 1], pred_decision))
print()
X = [[0,0], [1,1], [2,2], [3,3]]
Y = [-1, -1, -1, 1]
model = svm.LinearSVC()
model.fit(X, Y)
pred_decision = model.decision_function([[-1, -1], [4, 4]])
print(pred_decision)
y_true = [0, 3]
print(hinge_loss(y_true, pred_decision))
print结果:
[-2.18177262 2.36361684 0.09092211]
0.3030259636876493
[-1.45943282 0.70261777]
0.14869111490986564