Created
September 16, 2018 23:16
-
-
Save jsun/f2e26fb8b6052a1402db7d80a5348c5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn import datasets | |
from sklearn.pipeline import Pipeline | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.decomposition import PCA | |
from sklearn.svm import SVC | |
from sklearn.learning_curve import validation_curve | |
import matplotlib.pyplot as plt | |
import numpy as np | |
cancer = datasets.load_breast_cancer() | |
x = cancer.data | |
y = cancer.target | |
print(x.shape) | |
## (569, 30) | |
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) | |
print(x_train.shape) | |
## (455, 30) | |
ppln = Pipeline([ | |
('scale', StandardScaler()), | |
('pca', PCA(0.80)), | |
('clf', SVC()) | |
]) | |
svc_c_range = [0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0] | |
train_scores, valid_scores = validation_curve(estimator=ppln, | |
X=x_train, y=y_train, | |
param_name='clf__C', | |
param_range=svc_c_range, | |
cv=5, n_jobs=1) | |
# calculate the coorinates for plots | |
train_mean = np.mean(train_scores, axis=1) | |
train_std = np.std(train_scores, axis=1) | |
valid_mean = np.mean(valid_scores, axis=1) | |
valid_std = np.std(valid_scores, axis=1) | |
plt.style.use('seaborn-whitegrid') | |
# draw the training scores | |
plt.plot(svc_c_range, train_mean, color='orange', marker='o', markersize=5, label='training accuracy') | |
plt.fill_between(svc_c_range, train_mean + train_std, train_mean - train_std, alpha=0.1, color='orange') | |
# draw the validation scores | |
plt.plot(svc_c_range, valid_mean, color='darkblue', marker='o', markersize=5,label='validation accuracy') | |
plt.fill_between(svc_c_range, valid_mean + valid_std,valid_mean - valid_std, alpha=0.1, color='darkblue') | |
plt.xlabel('parameter C') | |
plt.xscale('log') | |
plt.ylabel('accuracy') | |
plt.legend(loc='lower right') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment