Created
September 16, 2018 22:48
-
-
Save jsun/ecdcc7de2212563e1a0b877845969caa 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.ensemble import RandomForestClassifier | |
from sklearn.learning_curve import learning_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', RandomForestClassifier(max_depth=3)) | |
]) | |
train_sizes, train_scores, valid_scores = learning_curve(estimator=ppln, | |
X=x_train, y=y_train, | |
train_sizes=np.linspace(0.1, 1.0, 10), | |
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(train_sizes, train_mean, color='orange', marker='o', markersize=5, label='training accuracy') | |
plt.fill_between(train_sizes, train_mean + train_std, train_mean - train_std, alpha=0.1, color='orange') | |
# draw the validation scores | |
plt.plot(train_sizes, valid_mean, color='darkblue', marker='o', markersize=5,label='validation accuracy') | |
plt.fill_between(train_sizes, valid_mean + valid_std,valid_mean - valid_std, alpha=0.1, color='darkblue') | |
plt.xlabel('#training samples') | |
plt.ylabel('accuracy') | |
plt.legend(loc='lower right') | |
plt.ylim([0.7, 1.01]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment