📚 The CoCalc Library - books, templates and other resources
License: OTHER
import matplotlib.pyplot as plt123def make_bracket(s, xy, textxy, width, ax):4annotation = ax.annotate(5s, xy, textxy, ha="center", va="center", size=20,6arrowprops=dict(arrowstyle="-[", fc="w", ec="k",7lw=2,), bbox=dict(boxstyle="square", fc="w"))8annotation.arrow_patch.get_arrowstyle().widthB = width91011def plot_improper_processing():12fig, axes = plt.subplots(2, 1, figsize=(15, 10))1314for axis in axes:15bars = axis.barh([0, 0, 0], [11.9, 2.9, 4.9], left=[0, 12, 15],16color=['white', 'grey', 'grey'], hatch="//",17align='edge', edgecolor='k')18bars[2].set_hatch(r"")19axis.set_yticks(())20axis.set_frame_on(False)21axis.set_ylim(-.1, 6)22axis.set_xlim(-0.1, 20.1)23axis.set_xticks(())24axis.tick_params(length=0, labeltop=True, labelbottom=False)25axis.text(6, -.3, "training folds",26fontdict={'fontsize': 14}, horizontalalignment="center")27axis.text(13.5, -.3, "validation fold",28fontdict={'fontsize': 14}, horizontalalignment="center")29axis.text(17.5, -.3, "test set",30fontdict={'fontsize': 14}, horizontalalignment="center")3132make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[0])33make_bracket("SVC fit", (6, 3), (6, 4), 12, axes[0])34make_bracket("SVC predict", (13.4, 3), (13.4, 4), 2.5, axes[0])3536axes[0].set_title("Cross validation")37axes[1].set_title("Test set prediction")3839make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[1])40make_bracket("SVC fit", (7.5, 3), (7.5, 4), 15, axes[1])41make_bracket("SVC predict", (17.5, 3), (17.5, 4), 4.8, axes[1])424344def plot_proper_processing():45fig, axes = plt.subplots(2, 1, figsize=(15, 8))4647for axis in axes:48bars = axis.barh([0, 0, 0], [11.9, 2.9, 4.9],49left=[0, 12, 15], color=['white', 'grey', 'grey'],50hatch="//", align='edge', edgecolor='k')51bars[2].set_hatch(r"")52axis.set_yticks(())53axis.set_frame_on(False)54axis.set_ylim(-.1, 4.5)55axis.set_xlim(-0.1, 20.1)56axis.set_xticks(())57axis.tick_params(length=0, labeltop=True, labelbottom=False)58axis.text(6, -.3, "training folds", fontdict={'fontsize': 14},59horizontalalignment="center")60axis.text(13.5, -.3, "validation fold", fontdict={'fontsize': 14},61horizontalalignment="center")62axis.text(17.5, -.3, "test set", fontdict={'fontsize': 14},63horizontalalignment="center")6465make_bracket("scaler fit", (6, 1.3), (6, 2.), 12, axes[0])66make_bracket("SVC fit", (6, 3), (6, 4), 12, axes[0])67make_bracket("SVC predict", (13.4, 3), (13.4, 4), 2.5, axes[0])6869axes[0].set_title("Cross validation")70axes[1].set_title("Test set prediction")7172make_bracket("scaler fit", (7.5, 1.3), (7.5, 2.), 15, axes[1])73make_bracket("SVC fit", (7.5, 3), (7.5, 4), 15, axes[1])74make_bracket("SVC predict", (17.5, 3), (17.5, 4), 4.8, axes[1])75fig.subplots_adjust(hspace=.3)767778