|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +PCovC Hyperparameter Tuning |
| 6 | +=========================== |
| 7 | +""" |
| 8 | +# %% |
| 9 | +# |
| 10 | + |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from matplotlib.colors import LinearSegmentedColormap |
| 13 | +from sklearn.datasets import load_iris |
| 14 | +from sklearn.decomposition import PCA |
| 15 | +from sklearn.inspection import DecisionBoundaryDisplay |
| 16 | +from sklearn.linear_model import LogisticRegressionCV, Perceptron, RidgeClassifierCV |
| 17 | +from sklearn.preprocessing import StandardScaler |
| 18 | +from sklearn.svm import LinearSVC |
| 19 | + |
| 20 | +from skmatter.decomposition import PCovC |
| 21 | + |
| 22 | + |
| 23 | +plt.rcParams["image.cmap"] = "tab10" |
| 24 | +plt.rcParams["scatter.edgecolors"] = "k" |
| 25 | + |
| 26 | +random_state = 10 |
| 27 | +n_components = 2 |
| 28 | + |
| 29 | +# %% |
| 30 | +# |
| 31 | +# For this, we will use the :func:`sklearn.datasets.load_iris` dataset from |
| 32 | +# ``sklearn``. |
| 33 | + |
| 34 | +X, y = load_iris(return_X_y=True) |
| 35 | + |
| 36 | +scaler = StandardScaler() |
| 37 | +X_scaled = scaler.fit_transform(X) |
| 38 | + |
| 39 | +# %% |
| 40 | +# |
| 41 | +# PCA |
| 42 | +# --- |
| 43 | +# |
| 44 | + |
| 45 | +pca = PCA(n_components=n_components) |
| 46 | + |
| 47 | +pca.fit(X_scaled, y) |
| 48 | +T_pca = pca.transform(X_scaled) |
| 49 | + |
| 50 | +fig, axis = plt.subplots() |
| 51 | +scatter = axis.scatter(T_pca[:, 0], T_pca[:, 1], c=y) |
| 52 | +axis.set(xlabel="PC$_1$", ylabel="PC$_2$") |
| 53 | +axis.legend( |
| 54 | + scatter.legend_elements()[0], |
| 55 | + load_iris().target_names, |
| 56 | + loc="lower right", |
| 57 | + title="Classes", |
| 58 | +) |
| 59 | + |
| 60 | +# %% |
| 61 | +# |
| 62 | +# Effect of Mixing Parameter :math:`\alpha` on PCovC Map |
| 63 | +# ------------------------------------------------------ |
| 64 | +# |
| 65 | +# Below, we see how different :math:`\alpha` values for our PCovC model |
| 66 | +# result in varying class distinctions between setosa, versicolor, |
| 67 | +# and virginica on the PCovC map. |
| 68 | + |
| 69 | +n_mixing = 5 |
| 70 | +mixing_params = [0, 0.25, 0.50, 0.75, 1] |
| 71 | + |
| 72 | +fig, axs = plt.subplots(1, n_mixing, figsize=(4 * n_mixing, 4), sharey="row") |
| 73 | + |
| 74 | +for id in range(0, n_mixing): |
| 75 | + mixing = mixing_params[id] |
| 76 | + |
| 77 | + pcovc = PCovC( |
| 78 | + mixing=mixing, |
| 79 | + n_components=n_components, |
| 80 | + random_state=random_state, |
| 81 | + classifier=LogisticRegressionCV(), |
| 82 | + ) |
| 83 | + |
| 84 | + pcovc.fit(X_scaled, y) |
| 85 | + T = pcovc.transform(X_scaled) |
| 86 | + |
| 87 | + axs[id].set_xticks([]) |
| 88 | + axs[id].set_yticks([]) |
| 89 | + |
| 90 | + axs[id].set_title(r"$\alpha=$" + str(mixing)) |
| 91 | + axs[id].set_xlabel("PCov$_1$") |
| 92 | + axs[id].scatter(T[:, 0], T[:, 1], c=y) |
| 93 | + |
| 94 | +axs[0].set_ylabel("PCov$_2$") |
| 95 | + |
| 96 | +fig.subplots_adjust(wspace=0) |
| 97 | + |
| 98 | +# %% |
| 99 | +# |
| 100 | +# Effect of PCovC Classifier on PCovC Map and Decision Boundaries |
| 101 | +# --------------------------------------------------------------- |
| 102 | +# |
| 103 | +# Here, we see how a PCovC model (:math:`\alpha` = 0.5) fitted with |
| 104 | +# different classifiers produces varying PCovC maps. In addition, |
| 105 | +# we see the varying decision boundaries produced by the |
| 106 | +# respective PCovC classifiers. |
| 107 | + |
| 108 | +mixing = 0.5 |
| 109 | +fig, axs = plt.subplots(1, 4, figsize=(16, 4)) |
| 110 | + |
| 111 | +models = { |
| 112 | + RidgeClassifierCV(): "Ridge Classification", |
| 113 | + LogisticRegressionCV(random_state=random_state): "Logistic Regression", |
| 114 | + LinearSVC(random_state=random_state): "Support Vector Classification", |
| 115 | + Perceptron(random_state=random_state): "Single-Layer Perceptron", |
| 116 | +} |
| 117 | + |
| 118 | +for id in range(0, len(models)): |
| 119 | + model = list(models)[id] |
| 120 | + |
| 121 | + pcovc = PCovC( |
| 122 | + mixing=mixing, |
| 123 | + n_components=n_components, |
| 124 | + random_state=random_state, |
| 125 | + classifier=model, |
| 126 | + ) |
| 127 | + |
| 128 | + pcovc.fit(X_scaled, y) |
| 129 | + T = pcovc.transform(X_scaled) |
| 130 | + |
| 131 | + graph = axs[id] |
| 132 | + graph.set_title(models[model]) |
| 133 | + |
| 134 | + DecisionBoundaryDisplay.from_estimator( |
| 135 | + estimator=pcovc.classifier_, |
| 136 | + X=T, |
| 137 | + ax=graph, |
| 138 | + response_method="predict", |
| 139 | + grid_resolution=1000, |
| 140 | + ) |
| 141 | + |
| 142 | + scatter = graph.scatter(T[:, 0], T[:, 1], c=y) |
| 143 | + |
| 144 | + graph.set_xlabel("PCov$_1$") |
| 145 | + graph.set_xticks([]) |
| 146 | + graph.set_yticks([]) |
| 147 | + |
| 148 | +axs[0].set_ylabel("PCov$_2$") |
| 149 | +axs[0].legend( |
| 150 | + scatter.legend_elements()[0], |
| 151 | + load_iris().target_names, |
| 152 | + loc="lower right", |
| 153 | + title="Classes", |
| 154 | + fontsize=8, |
| 155 | +) |
| 156 | + |
| 157 | +fig.subplots_adjust(wspace=0.04) |
| 158 | +plt.show() |
0 commit comments