📚 The CoCalc Library - books, templates and other resources
License: OTHER
import numpy as np1import matplotlib.pyplot as plt23from .tools import plot_2d_separator, plot_2d_scores, cm, discrete_scatter4from .plot_helpers import ReBl567def plot_confusion_matrix_illustration():8plt.figure(figsize=(8, 8))9confusion = np.array([[401, 2], [8, 39]])10plt.text(0.40, .7, confusion[0, 0], size=70, horizontalalignment='right')11plt.text(0.40, .2, confusion[1, 0], size=70, horizontalalignment='right')12plt.text(.90, .7, confusion[0, 1], size=70, horizontalalignment='right')13plt.text(.90, 0.2, confusion[1, 1], size=70, horizontalalignment='right')14plt.xticks([.25, .75], ["predicted 'not nine'", "predicted 'nine'"], size=20)15plt.yticks([.25, .75], ["true 'nine'", "true 'not nine'"], size=20)16plt.plot([.5, .5], [0, 1], '--', c='k')17plt.plot([0, 1], [.5, .5], '--', c='k')1819plt.xlim(0, 1)20plt.ylim(0, 1)212223def plot_binary_confusion_matrix():24plt.text(0.45, .6, "TN", size=100, horizontalalignment='right')25plt.text(0.45, .1, "FN", size=100, horizontalalignment='right')26plt.text(.95, .6, "FP", size=100, horizontalalignment='right')27plt.text(.95, 0.1, "TP", size=100, horizontalalignment='right')28plt.xticks([.25, .75], ["predicted negative", "predicted positive"], size=15)29plt.yticks([.25, .75], ["positive class", "negative class"], size=15)30plt.plot([.5, .5], [0, 1], '--', c='k')31plt.plot([0, 1], [.5, .5], '--', c='k')3233plt.xlim(0, 1)34plt.ylim(0, 1)353637def plot_decision_threshold():38from sklearn.datasets import make_blobs39from sklearn.svm import SVC40from sklearn.model_selection import train_test_split4142X, y = make_blobs(n_samples=(400, 50), cluster_std=[7.0, 2],43random_state=22)44X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)4546fig, axes = plt.subplots(2, 3, figsize=(15, 8), subplot_kw={'xticks': (), 'yticks': ()})47plt.suptitle("decision_threshold")48axes[0, 0].set_title("training data")49discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 0])5051svc = SVC(gamma=.05).fit(X_train, y_train)52axes[0, 1].set_title("decision with threshold 0")53discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 1])54plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,55ax=axes[0, 1], cm=ReBl)56plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 1])57axes[0, 2].set_title("decision with threshold -0.8")58discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 2])59plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 2], threshold=-.8)60plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,61ax=axes[0, 2], cm=ReBl)6263axes[1, 0].set_axis_off()6465mask = np.abs(X_train[:, 1] - 7) < 566bla = np.sum(mask)6768line = np.linspace(X_train.min(), X_train.max(), 100)69axes[1, 1].set_title("Cross-section with threshold 0")70axes[1, 1].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')71dec = svc.decision_function(np.c_[line, 10 * np.ones(100)])72contour = (dec > 0).reshape(1, -1).repeat(10, axis=0)73axes[1, 1].contourf(line, np.linspace(-1.5, 1.5, 10), contour, alpha=0.4, cmap=cm)74discrete_scatter(X_train[mask, 0], np.zeros(bla), y_train[mask], ax=axes[1, 1])75axes[1, 1].set_xlim(X_train.min(), X_train.max())76axes[1, 1].set_ylim(-1.5, 1.5)77axes[1, 1].set_xticks(())78axes[1, 1].set_ylabel("Decision value")7980contour2 = (dec > -.8).reshape(1, -1).repeat(10, axis=0)81axes[1, 2].set_title("Cross-section with threshold -0.8")82axes[1, 2].contourf(line, np.linspace(-1.5, 1.5, 10), contour2, alpha=0.4, cmap=cm)83discrete_scatter(X_train[mask, 0], np.zeros(bla), y_train[mask], alpha=.1, ax=axes[1, 2])84axes[1, 2].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')85axes[1, 2].set_xlim(X_train.min(), X_train.max())86axes[1, 2].set_ylim(-1.5, 1.5)87axes[1, 2].set_xticks(())88axes[1, 2].set_ylabel("Decision value")89axes[1, 0].legend(['negative class', 'positive class'])909192