Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book1/04/hinge_loss_plot.ipynb
1193 views
Kernel: Unknown Kernel
# Plots 0-1, hinge and log loss. import numpy as np import matplotlib.pyplot as plt try: import probml_utils as pml except ModuleNotFoundError: %pip install -qq git+https://github.com/probml/probml-utils.git import probml_utils as pml zeroOne = np.vectorize(lambda x: 1 * (x <= 0)) hinge = np.vectorize(lambda x: max(0, 1 - x)) logLoss = np.vectorize(lambda x: np.log2(1 + np.exp(-x))) expLoss = np.vectorize(lambda x: np.exp(-x)) funs = [zeroOne, hinge, logLoss, expLoss] styles = ["k-", "b:", "r-.", "g-"] labels = ["0-1 loss", "hinge loss", "log loss", "exp loss"] x = np.arange(-2, 2, 0.01) for i, fun in enumerate(funs): plt.plot(x, fun(x), styles[i], label=labels[i], linewidth=2) plt.axis([-2.1, 2.1, -0.1, 3.1]) plt.legend(fontsize=12) pml.savefig("hingeLoss.pdf") plt.show()