Machine learning > Support Vector Machines > SVM Theory and Usage > Linear SVM
Linear Support Vector Machines (SVM)
Introduction to Linear SVM
The Core Concept: Maximal Margin
Mathematical Formulation (Simplified)
Code Snippet: Linear SVM with Scikit-learn
python
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
# Sample data (replace with your actual data)
X = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9,11]])
y = np.array([0, 0, 1, 1, 0, 1]) # 0: class 1, 1: class 2
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a linear SVM classifier
clf = svm.SVC(kernel='linear')
# Train the classifier
clf.fit(X_train, y_train)
# Make predictions on the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
# Print support vectors
print("Support Vectors:", clf.support_vectors_)
Concepts Behind the Snippet
Real-Life Use Case: Text Classification
Best Practices
Interview Tip
When to Use Linear SVMs
Memory Footprint
Alternatives
The choice of algorithm depends on the specific characteristics of the data and the problem.
Pros of Linear SVMs
Cons of Linear SVMs
FAQ
-
What is the difference between a linear SVM and a non-linear SVM?
A linear SVM uses a linear kernel to find a hyperplane that separates the data, while a non-linear SVM uses non-linear kernels (e.g., RBF, polynomial) to map the data into a higher-dimensional space where it can be linearly separated. -
What is the role of the 'C' parameter in SVM?
The 'C' parameter is a regularization parameter that controls the trade-off between maximizing the margin and minimizing the classification error. A smaller value of C allows for a larger margin but may result in more misclassifications, while a larger value of C aims to classify all training examples correctly but may result in a smaller margin and overfitting. -
How do I choose the right kernel for my SVM?
The choice of kernel depends on the characteristics of your data. If the data is linearly separable, a linear kernel is a good choice. If the data is non-linearly separable, you can try different non-linear kernels (e.g., RBF, polynomial) and evaluate their performance using cross-validation. In general, RBF kernel is a good starting point for non-linear data. -
Why is feature scaling important for SVM?
Feature scaling is important for SVM because SVMs are sensitive to the scale of the features. If the features have different ranges, the features with larger ranges may dominate the distance calculations, leading to poor performance. Scaling the features ensures that all features contribute equally to the model.