Course contentsShow
← Machine Learning and Deep Learning
Lesson 276 of 3,5385. Support Vector MachinesFree lesson

Training and Predicting with Linear SVMs

Implement linear SVM training using libraries and understand how to make predictions with the learned hyperplane.

Training and Predicting with Linear SVMs

What You'll Learn

You'll learn how to use Scikit-learn to train a linear SVM classifier and make predictions using the learned decision boundary.

From Theory to Practice

You've learned the mathematical foundations of SVMs—now it's time to put them into action. Training a linear SVM means finding the optimal hyperplane that maximizes the margin between classes, and the support vectors that define it. Fortunately, Scikit-learn handles all the complex optimization (Lagrange multipliers, dual formulation) behind the scenes.

Training a Linear SVM

In Scikit-learn, you use the LinearSVC class (SVC stands for Support Vector Classification). The most important hyperparameter is C, which controls the soft-margin tradeoff:

from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler

# Scale features (crucial for SVMs!)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)

# Train the linear SVM
svm = LinearSVC(C=1.0, max_iter=1000)
svm.fit(X_train_scaled, y_train)

Remember: Always scale your features before training SVMs since they're sensitive to feature magnitudes.

Making Predictions

Once trained, the model uses the learned hyperplane (stored in svm.coef_ and svm.intercept_) to classify new points:

X_test_scaled = scaler.transform(X_test)
predictions = svm.predict(X_test_scaled)

The prediction is based on which side of the decision boundary each point falls. The model returns the class label directly.

Think of It This Way

Training is like finding the perfect fence line that separates two groups with the widest safety zone. Predicting is simply checking which side of that fence a new point lands on.

Key Takeaway: Use LinearSVC with scaled features to train linear SVMs, tuning the C parameter to balance margin size and classification errors; predictions are made by evaluating which side of the learned hyperplane new points fall on.