()
| 67 | |
| 68 | |
| 69 | def plot_logistic(): |
| 70 | np.random.seed(12345) |
| 71 | |
| 72 | fig, axes = plt.subplots(4, 4) |
| 73 | for i, ax in enumerate(axes.flatten()): |
| 74 | n_in = 1 |
| 75 | n_ex = 150 |
| 76 | X_train, y_train, X_test, y_test = random_classification_problem( |
| 77 | n_ex, n_classes=2, n_in=n_in, seed=i |
| 78 | ) |
| 79 | LR = LogisticRegression(penalty="l2", gamma=0.2, fit_intercept=True) |
| 80 | LR.fit(X_train, y_train, lr=0.1, tol=1e-7, max_iter=1e7) |
| 81 | y_pred = (LR.predict(X_test) >= 0.5) * 1.0 |
| 82 | loss = zero_one_loss(y_test, y_pred) * 100.0 |
| 83 | |
| 84 | LR_sk = LogisticRegression_sk( |
| 85 | penalty="l2", tol=0.0001, C=0.8, fit_intercept=True, random_state=i |
| 86 | ) |
| 87 | LR_sk.fit(X_train, y_train) |
| 88 | y_pred_sk = (LR_sk.predict(X_test) >= 0.5) * 1.0 |
| 89 | loss_sk = zero_one_loss(y_test, y_pred_sk) * 100.0 |
| 90 | |
| 91 | xmin = min(X_test) - 0.1 * (max(X_test) - min(X_test)) |
| 92 | xmax = max(X_test) + 0.1 * (max(X_test) - min(X_test)) |
| 93 | X_plot = np.linspace(xmin, xmax, 100) |
| 94 | y_plot = LR.predict(X_plot) |
| 95 | y_plot_sk = LR_sk.predict_proba(X_plot.reshape(-1, 1))[:, 1] |
| 96 | |
| 97 | ax.scatter(X_test[y_pred == 0], y_test[y_pred == 0], alpha=0.5) |
| 98 | ax.scatter(X_test[y_pred == 1], y_test[y_pred == 1], alpha=0.5) |
| 99 | ax.plot(X_plot, y_plot, label="mine", alpha=0.75) |
| 100 | ax.plot(X_plot, y_plot_sk, label="sklearn", alpha=0.75) |
| 101 | ax.legend() |
| 102 | ax.set_title("Loss mine: {:.2f} Loss sklearn: {:.2f}".format(loss, loss_sk)) |
| 103 | |
| 104 | ax.xaxis.set_ticklabels([]) |
| 105 | ax.yaxis.set_ticklabels([]) |
| 106 | |
| 107 | plt.tight_layout() |
| 108 | plt.savefig("plot_logistic.png", dpi=300) |
| 109 | plt.close("all") |
| 110 | |
| 111 | |
| 112 | def plot_bayes(): |
nothing calls this directly
no test coverage detected