Sub topic 15
Decision scores and probability estimates
This chapter assumes you can fit an SVC in scikit-learn, know the soft-margin objective, and understand support vectors. Here you learn what an SVM outputs besides a class label, and why those outputs are not probabilities.
The decision function
A fitted SVM computes a signed score for each point: f(x) = w·x + b in the linear case, or the kernel sum over support vectors in the general case. The predicted class is the sign of f(x). The magnitude tells you how far the point is from the boundary, in units tied to the margin. Points with f(x) = ±1 lie on the margin. Values between -1 and +1 fall inside the margin.
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
X, y = make_blobs(n_samples=200, centers=2, cluster_std=2.0, random_state=0)
clf = SVC(kernel="linear", C=1.0).fit(X, y)
scores = clf.decision_function(X[:5])
print("scores:", scores.round(2))
print("labels:", clf.predict(X[:5]))The score is a ranking signal. A larger absolute value means a more confident placement relative to the boundary. It is not a calibrated chance of being correct.
Why scores are not probabilities
The hinge loss only cares whether a point is on the correct side of the margin. It never asks the model to say how likely a class is. Two points with scores of 3 and 30 are both "correct and outside the margin", and training treats them the same. So the scale of the score has no fixed link to probability, and it changes with C, gamma, and feature scaling.
Probability estimates with Platt scaling
Setting probability=True makes scikit-learn fit a sigmoid on the decision scores, called Platt scaling. It learns parameters A and B so that P(y=1 | x) = 1 / (1 + exp(A·f(x) + B)). The fit uses internal 5-fold cross-validation.
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
X, y = make_moons(n_samples=400, noise=0.3, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = SVC(kernel="rbf", probability=True, random_state=0).fit(X_train, y_train)
print(clf.predict_proba(X_test[:3]).round(3))
print(clf.predict(X_test[:3]))Common errors
- Expecting
predictto matchpredict_proba. The sigmoid and the cross-validation are fitted separately from the boundary, so on points near the boundary the argmax of the probabilities can disagree withpredict. - Forgetting the cost.
probability=Truemultiplies training time by roughly five and adds memory use. Enable it only if you need it. - Reading scores as confidence across models. Scores from models with different C or gamma are on different scales.
- Using
probability=Truewith a fixed seed omitted. The internal cross-validation is random, so results vary between runs unless you setrandom_state.
Exercise
A point has decisionfunction value 0.4 under an SVC with probability=True, and predictproba gives 0.48 for the positive class. predict returns the positive class. Explain how this can happen and which output you would trust to choose a custom decision threshold.
Show answer
predict uses the sign of the decision score (0.4 > 0, so positive). predictproba uses a separate sigmoid fitted with internal cross-validation, so its 0.5 crossing does not have to sit exactly at score 0. This gives a small disagreement near the boundary. For a custom threshold, use decisionfunction directly, or calibrate the probabilities on held-out data if you need real probabilities.
Exercise
Why does training an SVC with probability=True take noticeably longer than with probability=False?
Show answer
Platt scaling fits its sigmoid on scores produced by internal 5-fold cross-validation, so the SVM is trained several extra times.