1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
- import sys
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.naive_bayes import GaussianNB
- from sklearn.svm import SVC
- from sklearn.model_selection import ShuffleSplit
- from sklearn.linear_model import LinearRegression
- from sklearn.learning_curve import learning_curve
- from sklearn.metrics import explained_variance_score, make_scorer
- from sklearn.cross_validation import KFold
- def set_trace():
- """A Poor mans break point"""
- # without this in iPython debugger can generate strange characters.
- from IPython.core.debugger import Pdb
- Pdb().set_trace(sys._getframe().f_back)
- print(__doc__)
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.naive_bayes import GaussianNB
- from sklearn.svm import SVC
- from sklearn.datasets import load_digits
- from sklearn.model_selection import learning_curve
- from sklearn.model_selection import ShuffleSplit
- def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
- plt.figure()
- plt.title(title)
- if ylim is not None:
- plt.ylim(*ylim)
- plt.xlabel("Training examples")
- plt.ylabel("Score")
- train_sizes, train_scores, test_scores = learning_curve(
- estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
- 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)
- plt.grid()
- plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
- train_scores_mean + train_scores_std, alpha=0.1,
- color="r")
- plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
- test_scores_mean + test_scores_std, alpha=0.1, color="g")
- plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
- label="Training score")
- plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
- label="Cross-validation score")
- plt.legend(loc="best")
- return plt
- def get_learning_curve(X, y):
- title = "Learning Curves (Naive Bayes)"
- # Cross validation with 100 iterations to get smoother mean test and train
- # score curves, each time with 20% data randomly selected as a validation set.
- title = "Learning Curves (SVM, RBF kernel, $\gamma=0.001$)"
- # SVC is more expensive so we do a lower number of CV iterations:
- cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)
- estimator = SVC(gamma=0.001)
- plt = plot_learning_curve(estimator, title, X, y, cv=None, n_jobs=1)
- plt.show()
- set_trace()
- '''
- def plot_learning_curve(x_std, y):
- size = 1000
- cv = KFold(size, shuffle=True)
- X = np.reshape(np.random.normal(scale=2, size=size), (-1, 1))
- X.shape
- y = np.array([[1 - 2 * x[0] + x[0]**2] for x in X])
- y.shape
-
- lg = LinearRegression()
- lg.fit(X, y)
- train_sizes, train_scores, test_scores = learning_curve(
- lg, X, y, n_jobs=-1, cv=cv, train_sizes=np.linspace(.1, 1.0, 5), verbose=0)
- 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)
- plt.figure()
- plt.title("Linear Regression")
- plt.legend(loc="best")
- plt.xlabel("Training examples")
- plt.ylabel("Score")
- plt.gca().invert_yaxis()
- # box-like grid
- plt.grid()
- # plot the std deviation as a transparent range at each training set size
- plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
- train_scores_mean + train_scores_std, alpha=0.1, color="r")
- plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
- test_scores_mean + test_scores_std, alpha=0.1, color="g")
- # plot the average training and test score lines at each training set size
- plt.plot(train_sizes, train_scores_mean, 'o-',
- color="r", label="Training score")
- plt.plot(train_sizes, test_scores_mean, 'o-',
- color="g", label="Cross-validation score")
- # sizes the window for readability and displays the plot
- # shows error from 0 to 1.1
- plt.ylim(-.1, 1.1)
- plt.show()
- '''
- '''
- def plot_learning_curve(estimator, title, x_std, y, ylim=None, cv=None, n_jobs=1):
- train_sizes = 100
- plt.figure()
- plt.title(title)
- if ylim is not None:
- plt.ylim(*ylim)
- plt.xlabel("Training examples")
- plt.ylabel("Score")
- set_trace()
- train_sizes, train_scores, test_scores = learning_curve(estimator, x_std, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
- 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)
- plt.grid()
- plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
- train_scores_mean + train_scores_std, alpha=0.1,
- color="r")
- plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
- test_scores_mean + test_scores_std, alpha=0.1, color="g")
- plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
- label="Training score")
- plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
- label="Cross-validation score")
- plt.legend(loc="best")
- return plt
-
- title = "Learning Curves (Naive Bayes)"
- # Cross validation with 100 iterations to get smoother mean test and train
- # score curves, each time with 20% data randomly selected as a validation set.
- #cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
- cv = KFold(train_sizes, shuffle=True)
- estimator = GaussianNB()
- plot_learning_curve(estimator, title, x_std, y, ylim=(0.7, 1.01), cv=cv, n_jobs=4)
- title = "Learning Curves (SVM, RBF kernel, $\gamma=0.001$)"
- # SVC is more expensive so we do a lower number of CV iterations:
- cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)
- estimator = SVC(gamma=0.001)
- plot_learning_curve(estimator, title, x_std, y, (0.7, 1.01), cv=cv, n_jobs=4)
- plt.show()
- '''
|