Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
import matplotlib.pyplot as plt
2
3
4
def make_bracket(s, xy, textxy, width, ax):
5
annotation = ax.annotate(
6
s, xy, textxy, ha="center", va="center", size=20,
7
arrowprops=dict(arrowstyle="-[", fc="w", ec="k",
8
lw=2,), bbox=dict(boxstyle="square", fc="w"))
9
annotation.arrow_patch.get_arrowstyle().widthB = width
10
11
12
def plot_improper_processing():
13
fig, axes = plt.subplots(2, 1, figsize=(15, 10))
14
15
for axis in axes:
16
bars = axis.barh([0, 0, 0], [11.9, 2.9, 4.9], left=[0, 12, 15],
17
color=['white', 'grey', 'grey'], hatch="//",
18
align='edge', edgecolor='k')
19
bars[2].set_hatch(r"")
20
axis.set_yticks(())
21
axis.set_frame_on(False)
22
axis.set_ylim(-.1, 6)
23
axis.set_xlim(-0.1, 20.1)
24
axis.set_xticks(())
25
axis.tick_params(length=0, labeltop=True, labelbottom=False)
26
axis.text(6, -.3, "training folds",
27
fontdict={'fontsize': 14}, horizontalalignment="center")
28
axis.text(13.5, -.3, "validation fold",
29
fontdict={'fontsize': 14}, horizontalalignment="center")
30
axis.text(17.5, -.3, "test set",
31
fontdict={'fontsize': 14}, horizontalalignment="center")
32
33
make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[0])
34
make_bracket("SVC fit", (6, 3), (6, 4), 12, axes[0])
35
make_bracket("SVC predict", (13.4, 3), (13.4, 4), 2.5, axes[0])
36
37
axes[0].set_title("Cross validation")
38
axes[1].set_title("Test set prediction")
39
40
make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[1])
41
make_bracket("SVC fit", (7.5, 3), (7.5, 4), 15, axes[1])
42
make_bracket("SVC predict", (17.5, 3), (17.5, 4), 4.8, axes[1])
43
44
45
def plot_proper_processing():
46
fig, axes = plt.subplots(2, 1, figsize=(15, 8))
47
48
for axis in axes:
49
bars = axis.barh([0, 0, 0], [11.9, 2.9, 4.9],
50
left=[0, 12, 15], color=['white', 'grey', 'grey'],
51
hatch="//", align='edge', edgecolor='k')
52
bars[2].set_hatch(r"")
53
axis.set_yticks(())
54
axis.set_frame_on(False)
55
axis.set_ylim(-.1, 4.5)
56
axis.set_xlim(-0.1, 20.1)
57
axis.set_xticks(())
58
axis.tick_params(length=0, labeltop=True, labelbottom=False)
59
axis.text(6, -.3, "training folds", fontdict={'fontsize': 14},
60
horizontalalignment="center")
61
axis.text(13.5, -.3, "validation fold", fontdict={'fontsize': 14},
62
horizontalalignment="center")
63
axis.text(17.5, -.3, "test set", fontdict={'fontsize': 14},
64
horizontalalignment="center")
65
66
make_bracket("scaler fit", (6, 1.3), (6, 2.), 12, axes[0])
67
make_bracket("SVC fit", (6, 3), (6, 4), 12, axes[0])
68
make_bracket("SVC predict", (13.4, 3), (13.4, 4), 2.5, axes[0])
69
70
axes[0].set_title("Cross validation")
71
axes[1].set_title("Test set prediction")
72
73
make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[1])
74
make_bracket("SVC fit", (7.5, 3), (7.5, 4), 15, axes[1])
75
make_bracket("SVC predict", (17.5, 3), (17.5, 4), 4.8, axes[1])
76
fig.subplots_adjust(hspace=.3)
77
78