2014-01-23 05:03:15 +08:00
|
|
|
"""
|
|
|
|
|
==========================
|
|
|
|
|
Plotting Validation Curves
|
|
|
|
|
==========================
|
|
|
|
|
|
|
|
|
|
In this plot you can see the training scores and validation scores of an SVM
|
|
|
|
|
for different values of the kernel parameter gamma. For very low values of
|
|
|
|
|
gamma, you can see that both the training score and the validation score are
|
|
|
|
|
low. This is called underfitting. Medium values of gamma will result in high
|
2015-01-16 04:09:35 +08:00
|
|
|
values for both scores, i.e. the classifier is performing fairly well. If gamma
|
2014-01-23 05:03:15 +08:00
|
|
|
is too high, the classifier will overfit, which means that the training score
|
|
|
|
|
is good but the validation score is poor.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2014-01-23 05:03:15 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
import numpy as np
|
2015-10-24 00:40:11 +08:00
|
|
|
|
2014-01-23 05:03:15 +08:00
|
|
|
from sklearn.datasets import load_digits
|
|
|
|
|
from sklearn.svm import SVC
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import validation_curve
|
2014-01-23 05:03:15 +08:00
|
|
|
|
2019-07-14 23:10:54 +08:00
|
|
|
X, y = load_digits(return_X_y=True)
|
2021-11-17 01:52:19 +08:00
|
|
|
subset_mask = np.isin(y, [1, 2]) # binary classification: 1 vs 2
|
|
|
|
|
X, y = X[subset_mask], y[subset_mask]
|
2014-01-23 05:03:15 +08:00
|
|
|
|
|
|
|
|
param_range = np.logspace(-6, -1, 5)
|
|
|
|
|
train_scores, test_scores = validation_curve(
|
|
|
|
|
SVC(),
|
|
|
|
|
X,
|
|
|
|
|
y,
|
|
|
|
|
param_name="gamma",
|
|
|
|
|
param_range=param_range,
|
2019-07-14 22:26:41 +08:00
|
|
|
scoring="accuracy",
|
2021-11-17 01:52:19 +08:00
|
|
|
n_jobs=2,
|
2019-07-14 22:26:41 +08:00
|
|
|
)
|
2014-02-05 05:30:30 +08:00
|
|
|
train_scores_mean = np.mean(train_scores, axis=1)
|
|
|
|
|
train_scores_std = np.std(train_scores, axis=1)
|
|
|
|
|
test_scores_mean = np.mean(test_scores, axis=1)
|
|
|
|
|
test_scores_std = np.std(test_scores, axis=1)
|
2014-01-23 05:03:15 +08:00
|
|
|
|
|
|
|
|
plt.title("Validation Curve with SVM")
|
2019-01-05 12:22:36 +08:00
|
|
|
plt.xlabel(r"$\gamma$")
|
2014-01-23 05:03:15 +08:00
|
|
|
plt.ylabel("Score")
|
|
|
|
|
plt.ylim(0.0, 1.1)
|
2015-10-24 00:40:11 +08:00
|
|
|
lw = 2
|
|
|
|
|
plt.semilogx(
|
|
|
|
|
param_range, train_scores_mean, label="Training score", color="darkorange", lw=lw
|
|
|
|
|
)
|
2014-02-05 05:30:30 +08:00
|
|
|
plt.fill_between(
|
|
|
|
|
param_range,
|
|
|
|
|
train_scores_mean - train_scores_std,
|
2015-10-24 00:40:11 +08:00
|
|
|
train_scores_mean + train_scores_std,
|
|
|
|
|
alpha=0.2,
|
|
|
|
|
color="darkorange",
|
|
|
|
|
lw=lw,
|
|
|
|
|
)
|
2014-02-06 06:08:12 +08:00
|
|
|
plt.semilogx(
|
|
|
|
|
param_range, test_scores_mean, label="Cross-validation score", color="navy", lw=lw
|
2015-10-24 00:40:11 +08:00
|
|
|
)
|
2014-02-05 05:30:30 +08:00
|
|
|
plt.fill_between(
|
|
|
|
|
param_range,
|
|
|
|
|
test_scores_mean - test_scores_std,
|
2015-10-24 00:40:11 +08:00
|
|
|
test_scores_mean + test_scores_std,
|
|
|
|
|
alpha=0.2,
|
|
|
|
|
color="navy",
|
|
|
|
|
lw=lw,
|
|
|
|
|
)
|
2014-01-23 05:03:15 +08:00
|
|
|
plt.legend(loc="best")
|
|
|
|
|
plt.show()
|