Python ML Support Vector Machines
A support vector machine is a model that draws the widest gap between two classes. A kernel is a rule that reshapes the data so a straight gap can separate curved groups.
A straight gap first
kernel='linear' draws a straight gap. It scores well on the Digits test rows already.
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
data = load_digits()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.svm import SVC
model = SVC(kernel='linear')
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))A curved gap scores higher
kernel='rbf' reshapes the data first. The curved gap scores a little higher on the same test rows.
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
data = load_digits()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.svm import SVC
model = SVC(kernel='rbf')
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))Count the support vectors
support_vectors_ holds the rows closest to the gap. Only these rows shape where the gap falls.
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
data = load_digits()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.svm import SVC
model = SVC(kernel='rbf')
model.fit(X_train, y_train)
print(len(model.support_vectors_))Example
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
data = load_digits()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
model = SVC(kernel="rbf")
model.fit(X_train, y_train)
print("Train accuracy:", round(model.score(X_train, y_train), 3))
print("Test accuracy:", round(model.score(X_test, y_test), 3))
print("Support vectors:", len(model.support_vectors_))
print("First predictions:", model.predict(X_test[:5]).tolist())
print("First true labels:", y_test[:5].tolist())Key points
- Support vector machine: a model that draws the widest gap between two classes
- Margin: the width of the gap between the classes
- Support vector: a row that sits closest to the gap and shapes where it falls
- Use SVC to build a support vector machine for classification
- Kernel: the rule that reshapes the data before the gap is drawn
- Use kernel='linear' for a straight gap in the raw features
- Use kernel='rbf' for curved groups that a straight gap cannot split
In this example, the data holds handwritten digit images with a label from 0 to 9. Twenty percent of the rows are held back for testing by the split. SVC with kernel='rbf' fits the training rows and scores 0.986 on the test rows. The model keeps 650 support vectors, and the first five test rows are all predicted correctly.
Note
Scaling comes in Lesson 32. The Digits features share one scale, so this lesson uses the raw features.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What is a support vector?