Summary - SVM
- Machine Learning course
- Intermediate
- Lesson 30/112
- 6 min
In this group you learned SVM, margin, kernel, and C parameter.
Summary: SVMs
In this group:
- An SVM picks the boundary farthest from the classes; the support vectors determine it.
- The margin is
2/‖w‖; in a soft margin the penalty for violations isC. - Kernels:
linear,poly,rbf; in RBF,gammasets the area of influence. - C and gamma are searched together on a logarithmic grid.
- Always a pipeline with a scaler; OvO for many classes;
SVRfor regression.
Practice C and gamma search
Run GridSearchCV(..., cv=5) for make_pipeline(StandardScaler(), SVC()) on the moon data with the grid {"svc__C": [0.1, 1, 10, 100], "svc__gamma": [0.1, 1, 10]}. Print best_params_ and the test accuracy with round(..., 3) on separate lines.
Starter code
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
X, y = make_moons(n_samples=300, noise=0.25, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
# buraya yazSample solution
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
X, y = make_moons(n_samples=300, noise=0.25, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
gs = GridSearchCV(make_pipeline(StandardScaler(), SVC()), {"svc__C": [0.1, 1, 10, 100], "svc__gamma": [0.1, 1, 10]}, cv=5)
gs.fit(X_train, y_train)
print(gs.best_params_)
print(round(gs.score(X_test, y_test), 3))Expected output
{'svc__C': 1, 'svc__gamma': 10}
0.989
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If an RBF SVM has training accuracy 1.0 and low test accuracy, which setting change should be tried first?
- Lowering gamma and/or C Correct answer
- Raising gamma
- Removing scaling
- Raising C
Explanation: Both reduce the model's flexibility and reduce overfitting.
2 Create the SVM regressor.
r = ____(C=10, epsilon=0.1)
r = SVR(C=10, epsilon=0.1)
Explanation: SVR penalises errors outside the epsilon tube.
3 Match the parameters.
| Term | Meaning |
|---|---|
C |
Violation penalty |
gamma |
Narrowness of influence |
kernel |
Similarity function |
epsilon |
Tube width |