Soft margin and C
Quick answer
Soft-margin SVMs allow training points to violate the canonical margin rather than forcing perfect separation. The C parameter weighs violation costs against the size of the weight vector. Choosing C is a model-selection decision, not a universal rule. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Calculate a soft-margin objective
w=[2,0]slack=[0,1,2]print(sum(v*v for v in w)/2+0.5*sum(slack))Observed local output:
3.5Regularization contributes two and the violations contribute 1.5.
How it works
For the binary linear problem, minimize 0.5*||w||² + Csum(xi_i), subject to y_i(w·x_i+b)>=1-xi_i and xi_i>=0. For fixed w,b and positive C, each optimal slack xi_i equals hinge loss. With norm 2, total slack 3 and C=0.5, the objective is 2+1.5=3.5. Larger C makes violations more expensive relative to the norm term; it need not improve held-out accuracy or monotonically increase the number of support vectors. Smaller C may favor a flatter decision surface while accepting mistakes. In the dual view, each training observation receives a multiplier alpha_i between 0 and C (unweighted case), with sum(alpha_iy_i)=0. The score is sum(alpha_iy_i*K(x_i,x))+b. Nonzero multipliers identify support vectors. The kernel K is a dot product in a feature space; its role becomes clearer next lesson. Margin violations can have alpha at the upper bound. An observation with alpha=0 does not directly contribute to the dual score, but changing the training set may change the fitted model. The exact soft-margin objective uses summed losses, so C values are not automatically comparable across objectives or data sizes. LinearSVC and linear-kernel SVC can differ due to objective and solver choices; do not treat them as identical.
Inspect the effect of C
from sklearn.svm import SVCX=[[-2],[-1],[1],[2]]; y=[-1,-1,1,1]for c in [0.1,10]: m=SVC(kernel="linear",C=c).fit(X,y) print(round(float(m.coef_[0,0]),3),len(m.support_))Observed local output:
0.5 41.0 2The smaller C accepts violations in this fixture; the larger C reaches the hard-margin solution.
Common mistakes
Do not set C=0 in SVC or assume larger C guarantees better accuracy. Do not compare objectives calculated with different C values as validation metrics. A support vector can be inside the margin or misclassified; it need not lie exactly on a margin plane.
Practice and next steps
Compare C=0.1 and C=10 on the native fixture. Calculate the hinge losses from the decision scores. Explain why training objective and held-out accuracy answer different questions. Complete the knowledge check and staged exercise. Next: Kernel intuition and RBF gamma.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Soft margin and C — knowledge check
Apply soft margin and c to new scenarios.
What is a soft-margin slack at the optimum for fixed w,b?
What does increasing C do?
Which observations directly contribute in the dual score?
Can a support vector be misclassified?
What selects C in a reliable workflow?