forked from amueller/introduction_to_ml_with_python
-
Notifications
You must be signed in to change notification settings - Fork 250
/
plot_cross_validation.py
226 lines (186 loc) · 8.81 KB
/
plot_cross_validation.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import numpy as np
import matplotlib.pyplot as plt
def plot_group_kfold():
from sklearn.model_selection import GroupKFold
groups = [0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3]
plt.figure(figsize=(10, 2))
plt.title("GroupKFold")
axes = plt.gca()
axes.set_frame_on(False)
axes.margins(0.01)
n_folds = 12
n_samples = 12
n_iter = 3
n_samples_per_fold = 1
cv = GroupKFold(n_splits=3)
mask = np.zeros((n_iter, n_samples))
for i, (train, test) in enumerate(cv.split(range(12), groups=groups)):
mask[i, train] = 1
mask[i, test] = 2
for i in range(n_folds):
# test is grey
colors = ["grey" if x == 2 else "white" for x in mask[:, i]]
# not selected has no hatch
boxes = axes.barh(y=range(n_iter), width=[1 - 0.1] * n_iter,
left=i * n_samples_per_fold, height=.6, color=colors,
hatch="//", edgecolor=["k"]*n_iter, align='edge')
for j in np.where(mask[:, i] == 0)[0]:
boxes[j].set_hatch("")
axes.barh(y=[n_iter] * n_folds, width=[1 - 0.1] * n_folds,
left=np.arange(n_folds) * n_samples_per_fold, height=.6,
color="w", edgecolor=['k']*n_folds, align='edge')
for i in range(12):
axes.text((i + .5) * n_samples_per_fold, 3.4, "%d" %
groups[i], horizontalalignment="center")
axes.invert_yaxis()
axes.set_xlim(0, n_samples + 1)
axes.set_ylabel("교차 검증 반복")
axes.set_xlabel("데이터 포인트")
axes.set_xticks(np.arange(n_samples) + .5)
axes.set_xticklabels(np.arange(1, n_samples + 1))
axes.set_yticks(np.arange(n_iter + 1) + .3)
axes.set_yticklabels(["분할 %d" % x for x in range(1, n_iter + 1)] + ["그룹"]);
plt.legend([boxes[0], boxes[1]], ["훈련 세트", "테스트 세트"], loc=(1, .3));
plt.tight_layout()
def plot_shuffle_split():
from sklearn.model_selection import ShuffleSplit
plt.figure(figsize=(10, 2))
plt.title("ShuffleSplit with 10 points"
", train_size=5, test_size=2, n_splits=4")
axes = plt.gca()
axes.set_frame_on(False)
axes.margins(0.01)
n_folds = 10
n_samples = 10
n_iter = 4
n_samples_per_fold = 1
ss = ShuffleSplit(n_splits=4, train_size=5, test_size=2, random_state=43)
mask = np.zeros((n_iter, n_samples))
for i, (train, test) in enumerate(ss.split(range(10))):
mask[i, train] = 1
mask[i, test] = 2
for i in range(n_folds):
# test is grey
colors = ["grey" if x == 2 else "white" for x in mask[:, i]]
# not selected has no hatch
boxes = axes.barh(y=range(n_iter), width=[1 - 0.1] * n_iter,
left=i * n_samples_per_fold, height=.6, color=colors,
hatch="//", edgecolor=['k']*n_iter, align='edge')
for j in np.where(mask[:, i] == 0)[0]:
boxes[j].set_hatch("")
axes.invert_yaxis()
axes.set_xlim(0, n_samples + 1)
axes.set_ylabel("교차 검증 반복")
axes.set_xlabel("데이터 포인트")
axes.set_xticks(np.arange(n_samples) + .5)
axes.set_xticklabels(np.arange(1, n_samples + 1))
axes.set_yticks(np.arange(n_iter) + .3)
axes.set_yticklabels(["분할 %d" % x for x in range(1, n_iter + 1)]);
# legend hacked for this random state
plt.legend([boxes[1], boxes[0], boxes[2]], ["훈련 세트", "테스트 세트", "미선택 세트"], loc=(1, .3));
plt.tight_layout()
# plt.savefig("images/06_shuffle_split.png")
# plt.close()
def plot_stratified_cross_validation():
fig, both_axes = plt.subplots(2, 1, figsize=(12, 5))
# plt.title("cross_validation_not_stratified")
axes = both_axes[0]
axes.set_title("순서대로 나열된 레이블에 적용한 기본 교차 검증")
axes.set_frame_on(False)
axes.margins(0.01)
n_folds = 3
n_samples = 150
n_samples_per_fold = n_samples / float(n_folds)
for i in range(n_folds):
colors = ["w"] * n_folds
colors[i] = "grey"
axes.barh(y=range(n_folds), width=[n_samples_per_fold - 1] *
n_folds, left=i * n_samples_per_fold, height=.6,
color=colors, hatch="//", edgecolor=['k']*n_folds, align='edge')
axes.barh(y=[n_folds] * n_folds, width=[n_samples_per_fold - 1] *
n_folds, left=np.arange(3) * n_samples_per_fold, height=.6,
color="w", edgecolor=['k']*n_folds, align='edge')
axes.invert_yaxis()
axes.set_xlim(0, n_samples + 1)
axes.set_ylabel("교차 검증 반복")
axes.set_xlabel("데이터 포인트")
axes.set_xticks(np.arange(n_samples_per_fold / 2., n_samples, n_samples_per_fold))
axes.set_xticklabels(["폴드 %d" % x for x in range(1, n_folds + 1)])
axes.set_yticks(np.arange(n_folds + 1) + .3)
axes.set_yticklabels(["분할 %d" % x for x in range(1, n_folds + 1)] + ["클래스 레이블"])
for i in range(3):
axes.text((i + .5) * n_samples_per_fold, 3.4, "클래스 %d" % i, horizontalalignment="center")
ax = both_axes[1]
ax.set_title("계층별 교차 검증")
ax.set_frame_on(False)
ax.invert_yaxis()
ax.set_xlim(0, n_samples + 1)
ax.set_ylabel("교차 검증 반복")
ax.set_xlabel("데이터 포인트")
ax.set_yticks(np.arange(n_folds + 1) + .3)
ax.set_yticklabels(["분할 %d" % x for x in range(1, n_folds + 1)] + ["클래스 레이블"]);
n_subsplit = n_samples_per_fold / 3.
for i in range(n_folds):
test_bars = ax.barh(
y=[i] * n_folds, width=[n_subsplit - 1] * n_folds,
left=np.arange(n_folds) * n_samples_per_fold + i * n_subsplit,
height=.6, color="grey", hatch="//", edgecolor=['k']*n_folds, align='edge')
w = 2 * n_subsplit - 1
ax.barh(y=[0] * n_folds, width=[w] * n_folds, left=np.arange(n_folds)
* n_samples_per_fold + (0 + 1) * n_subsplit, height=.6, color="w",
hatch="//", edgecolor=['k']*n_folds, align='edge')
ax.barh(y=[1] * (n_folds + 1), width=[w / 2., w, w, w / 2.],
left=np.maximum(0, np.arange(n_folds + 1) * n_samples_per_fold -
n_subsplit), height=.6, color="w", hatch="//",
edgecolor=['k']*n_folds, align='edge')
training_bars = ax.barh(y=[2] * n_folds, width=[w] * n_folds,
left=np.arange(n_folds) * n_samples_per_fold,
height=.6, color="w", hatch="//", edgecolor=['k']*n_folds, align='edge')
ax.barh(y=[n_folds] * n_folds, width=[n_samples_per_fold - 1] *
n_folds, left=np.arange(n_folds) * n_samples_per_fold, height=.6,
color="w", edgecolor=['k']*n_folds, align='edge')
for i in range(3):
ax.text((i + .5) * n_samples_per_fold, 3.4, "클래스 %d" % i, horizontalalignment="center")
# ax.set_ylim(4, -0.1)
ax.margins(0.01)
plt.legend([training_bars[0], test_bars[0]], ['훈련 데이터', '테스트 데이터'], loc=(1.05, 1), frameon=False);
fig.tight_layout()
def plot_cross_validation():
plt.figure(figsize=(12, 2))
plt.margins(0.01)
axes = plt.gca()
axes.set_frame_on(False)
n_folds = 5
n_samples = 25
n_samples_per_fold = n_samples / float(n_folds)
for i in range(n_folds):
colors = ["w"] * n_folds
colors[i] = "grey"
bars = plt.barh(
y=range(n_folds), width=[n_samples_per_fold - 0.1] * n_folds,
left=i * n_samples_per_fold, height=.6, color=colors, hatch="//",
edgecolor=['k']*n_folds, align='edge')
axes.invert_yaxis()
axes.set_xlim(0, n_samples + 1)
plt.ylabel("교차 검증 반복")
plt.xlabel("데이터 포인트")
plt.xticks(np.arange(n_samples_per_fold / 2., n_samples, n_samples_per_fold), ["폴드 %d" % x for x in range(1, n_folds + 1)])
plt.yticks(np.arange(n_folds) + .3, ["분할 %d" % x for x in range(1, n_folds + 1)])
plt.legend([bars[0], bars[4]], ['훈련 데이터', '테스트 데이터'], loc=(1.05, 0.4), frameon=False);
def plot_threefold_split():
plt.figure(figsize=(15, 1))
axis = plt.gca()
axis.margins(0.01)
bars = axis.barh([0, 0, 0], [11.9, 2.9, 4.9], left=[0, 12, 15], color=[
'white', 'grey', 'grey'], hatch="//", edgecolor='k', align='edge')
bars[2].set_hatch(r"")
axis.set_yticks(())
axis.set_frame_on(False)
# axis.set_ylim(-.1, .8)
# axis.set_xlim(-0.1, 20.1)
axis.set_xticks([6, 13.3, 17.5])
axis.set_xticklabels(["훈련 세트", "검증 세트", "테스트 세트"], fontdict={'fontsize': 20});
axis.tick_params(length=0, labeltop=True, labelbottom=False)
axis.text(6, -.3, "모델 훈련", fontdict={'fontsize': 13}, horizontalalignment="center")
axis.text(13.3, -.3, "매개변수 선택", fontdict={'fontsize': 13}, horizontalalignment="center")
axis.text(17.5, -.3, "모델 평가", fontdict={'fontsize': 13}, horizontalalignment="center")