Support vector machine
Метод опорных векторов
A supervised learning model that separates classes by looking for a boundary with a wide gap between them. The training examples that determine the boundary are called support vectors.
Also known as: SVM
The idea behind an SVM
Imagine plotting two kinds of parts by length and weight. If you can draw several straight lines that separate the two groups perfectly, which one should you pick? A support vector machine (SVM) picks the line with the most room to spare. It maximizes the margin, the gap between the closest examples on the two sides. With more than two features, the linear decision boundary is a hyperplane rather than a line.
Support vectors, soft margins, and kernels
In the perfectly separable case, the examples that hold the margin in place are the support vectors. Think of them as the points that stop you from widening the gap any further. Moving a point well outside the margin doesn't change the fitted boundary as long as the point stays on the correct side and outside the margin.
Real classes often overlap. A soft-margin SVM penalizes points that sit inside the margin, including points on the wrong side of the decision boundary. The parameter C controls how costly those violations are. A smaller C puts more emphasis on a wide margin and can let the model ignore a noisy example. A larger C puts more emphasis on fitting the training labels. With a soft margin, a support vector doesn't have to be correctly classified.
When a straight boundary isn't enough, a kernel lets the SVM learn a linear separator in a transformed feature space. The kernel trick computes inner products in that space without explicitly constructing the transformed feature vectors. The resulting boundary can be nonlinear in the original inputs. For the radial basis function (RBF) kernel, gamma controls how quickly similarity falls with distance. High gamma makes the effect of each example more local and can produce an overly intricate boundary.
Example: an RBF classifier in Python
Given numeric features X_train, their labels y_train, and held-out features X_test, use a pipeline to keep scaling and fitting together:
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
classifier = make_pipeline(StandardScaler(), SVC(kernel='rbf'))
classifier.fit(X_train, y_train)
predictions = classifier.predict(X_test)
Without scaling, a feature with a large numerical range can dominate the distances used by RBF. The pipeline learns scaling statistics from the training data and reuses them at prediction time. When tuning C and gamma with cross-validation, pass in the whole pipeline so each fold learns its own scaling statistics without seeing its validation examples.
When to use it and what interviewers ask
A linear SVM is a useful baseline for text classification with many sparse features, such as TF-IDF vectors. An RBF SVM is worth comparing against it when the data set is modest and a linear boundary seems too restrictive. Kernel SVMs can be expensive to train on large data sets, and prediction generally gets slower as the number of support vectors grows.
A common interview question is how a linear SVM differs from logistic regression when both draw a linear boundary. For standard hinge loss, an SVM gives no loss to a correctly classified point at or beyond the margin. Logistic loss still gives that point some weight, though the contribution shrinks as the model becomes more confident. Also distinguish a decision score from a probability: an SVM score can rank examples, but it isn't a class probability without a calibration step.