Support Vector Machine
A Support Vector Machine (SVM) is a classification algorithm that finds the optimal boundary, or hyperplane, that best separates data points belonging to different classes.
The Core Idea
Among all the possible boundaries that could separate two classes, SVM chooses the one that maximizes the distance (margin) to the nearest data points from each class.
Implementing in Python
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
df = pd.DataFrame({
"Feature1": [1, 2, 3, 6, 7, 8, 9, 10],
"Feature2": [2, 3, 2, 8, 9, 8, 9, 10],
"Class": [0, 0, 0, 1, 1, 1, 1, 1]
})
X = df[["Feature1", "Feature2"]]
y = df["Class"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
model = SVC(kernel="linear")
model.fit(X_train, y_train)
print(model.predict(X_test))
Why SVM Works Well
By focusing only on the points closest to the boundary (the support vectors), SVM tends to generalize well even in high-dimensional spaces, and is less affected by points far from the decision boundary.
SVM can be used for both classification and regression tasks, though it's most commonly associated with classification problems.
Coming Up Next
Next, you'll take a closer look at the hyperplane that SVM uses to separate classes.