Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: Set up Python
uses: actions/setup-python@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: setup Python
uses: actions/setup-python@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: Set up Python
uses: actions/setup-python@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
contents: write

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
fetch-depth: 0
- name: setup Python
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/tests-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.13"]
python-version:
- "3.10"
- "3.13"

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
Expand Down
11 changes: 8 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.10", "3.13"]
os:
- ubuntu-latest
- macos-latest
- windows-latest
python-version:
- "3.10"
- "3.13"

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
Expand Down
6 changes: 5 additions & 1 deletion examples/pcovc/KPCovC_Comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

random_state = 0
n_components = 2
scale_z = True

# %%
#
Expand Down Expand Up @@ -85,7 +86,7 @@
# Both PCA and PCovC fail to produce linearly separable latent space
# maps. We will need a kernel method to effectively separate the moon classes.

mixing = 0.10
mixing = 0.5
alpha_d = 0.5
alpha_p = 0.4

Expand All @@ -95,6 +96,7 @@
n_components=n_components,
random_state=random_state,
mixing=mixing,
scale_z=scale_z,
classifier=LinearSVC(),
): "PCovC",
}
Expand Down Expand Up @@ -138,6 +140,7 @@
random_state=random_state,
mixing=mixing,
center=center,
scale_z=scale_z,
**kernel_params,
): {"title": "Kernel PCovC", "eps": 2},
}
Expand Down Expand Up @@ -220,6 +223,7 @@
mixing=mixing,
classifier=model,
center=center,
scale_z=scale_z,
**models[model]["kernel_params"],
)
t_kpcovc_train = kpcovc.fit_transform(X_train_scaled, y_train)
Expand Down
6 changes: 4 additions & 2 deletions examples/pcovc/KPCovC_Hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
fig, axs = plt.subplots(2, len(kernels), figsize=(len(kernels) * 4, 8))

center = True
mixing = 0.10
mixing = 0.5
scale_z = True

for i, kernel in enumerate(kernels):
kpca = KernelPCA(
Expand All @@ -83,6 +84,7 @@
random_state=random_state,
**kernel_params.get(kernel, {}),
center=center,
scale_z=scale_z,
)
t_kpcovc = kpcovc.fit_transform(X_scaled, y)

Expand Down Expand Up @@ -118,7 +120,7 @@
kpcovc = KernelPCovC(
n_components=n_components,
random_state=random_state,
mixing=mixing,
mixing=0.1,
center=center,
kernel="rbf",
gamma=gamma,
Expand Down
2 changes: 2 additions & 0 deletions examples/pcovc/PCovC_Hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
n_components=n_components,
random_state=random_state,
classifier=LogisticRegressionCV(),
scale_z=True,
)

pcovc.fit(X_scaled, y)
Expand Down Expand Up @@ -120,6 +121,7 @@
n_components=n_components,
random_state=random_state,
classifier=model,
scale_z=True,
)

pcovc.fit(X_scaled, y)
Expand Down
131 changes: 131 additions & 0 deletions examples/pcovc/PCovC_multioutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/usr/bin/env python
# coding: utf-8

"""
Multioutput PCovC
=================
"""
# %%
#

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegressionCV
from sklearn.multioutput import MultiOutputClassifier

from skmatter.decomposition import PCovC

plt.rcParams["image.cmap"] = "tab10"
plt.rcParams["scatter.edgecolors"] = "k"
# %%
# For this, we will use the `sklearn.datasets.load_digits` dataset.
# This dataset contains 8x8 images of handwritten digits (0-9).
X, y = load_digits(return_X_y=True)
x_scaler = StandardScaler()
X_scaled = StandardScaler().fit_transform(X)

np.unique(y)
# %%
# Let's begin by trying to make a PCovC map to separate the digits.
# This is a one-label, ten-class classification problem.
pca = PCA(n_components=2)
T_pca = pca.fit_transform(X_scaled, y)

pcovc = PCovC(n_components=2, mixing=0.5)
T_pcovc = pcovc.fit_transform(X_scaled, y)

fig, axs = plt.subplots(1, 2, figsize=(10, 6))

scat_pca = axs[0].scatter(T_pca[:, 0], T_pca[:, 1], c=y)
scat_pcovc = axs[1].scatter(T_pcovc[:, 0], T_pcovc[:, 1], c=y)
fig.colorbar(scat_pca, ax=axs, orientation="horizontal")
fig.suptitle("Multiclass PCovC with One Label")

# %%
# Next, let's try a two-label classification problem, with both labels
# being binary classification tasks.

is_even = (y % 2).reshape(-1, 1)
is_less_than_five = (y < 5).reshape(-1, 1)

y2 = np.hstack([is_even, is_less_than_five])
y2.shape
# %%
# Here, we can build a map that considers both of these labels simultaneously.

clf = MultiOutputClassifier(estimator=LogisticRegressionCV())
pcovc = PCovC(n_components=2, mixing=0.5, classifier=clf)

T_pcovc = pcovc.fit_transform(X_scaled, y2)

fig, axs = plt.subplots(2, 3, figsize=(15, 10))
cmap1 = "Set1"
cmap2 = "Set2"
cmap3 = "tab10"

labels_list = [["Even", "Odd"], [">= 5", "< 5"]]

for i, c, cmap in zip(range(3), [is_even, is_less_than_five, y], [cmap1, cmap2, cmap3]):
scat_pca = axs[0, i].scatter(T_pca[:, 0], T_pca[:, 1], c=c, cmap=cmap)
axs[1, i].scatter(T_pcovc[:, 0], T_pcovc[:, 1], c=c, cmap=cmap)

if i == 0 or i == 1:
handles, _ = scat_pca.legend_elements()
labels = labels_list[i]
axs[0, i].legend(handles, labels)

axs[0, 0].set_title("Even/Odd")
axs[0, 1].set_title("Greater/Less than 5")
axs[0, 2].set_title("Digit")

axs[0, 0].set_ylabel("PCA")
axs[1, 0].set_ylabel("PCovC")
fig.colorbar(scat_pca, ax=axs, orientation="horizontal")
fig.suptitle("Multilabel PCovC with Binary Labels")
# %%
# Let's try a more complicated example:

num_holes = np.array(
[0 if i in [1, 2, 3, 5, 7] else 1 if i in [0, 4, 6, 9] else 2 for i in y]
).reshape(-1, 1)

y3 = np.hstack([is_even, num_holes])
# %%
# Now, we have a two-label classification
# problem, with one binary label and one label with three
# possible classes.
clf = MultiOutputClassifier(estimator=LogisticRegressionCV())
pcovc = PCovC(n_components=2, mixing=0.5, classifier=clf)

T_pcovc = pcovc.fit_transform(X_scaled, y3)

fig, axs = plt.subplots(2, 3, figsize=(15, 10))
cmap1 = "Set1"
cmap2 = "Set3"
cmap3 = "tab10"

labels_list = [["Even", "Odd"], ["0", "1", "2"]]

for i, c, cmap in zip(range(3), [is_even, num_holes, y], [cmap1, cmap2, cmap3]):
scat_pca = axs[0, i].scatter(T_pca[:, 0], T_pca[:, 1], c=c, cmap=cmap)
axs[1, i].scatter(T_pcovc[:, 0], T_pcovc[:, 1], c=c, cmap=cmap)

if i == 0 or i == 1:
handles, _ = scat_pca.legend_elements()
labels = labels_list[i]
axs[0, i].legend(handles, labels)

axs[0, 0].set_title("Even/Odd")
axs[0, 1].set_title("Number of Holes")
axs[0, 2].set_title("Digit")

axs[0, 0].set_ylabel("PCA")
axs[1, 0].set_ylabel("PCovC")
fig.colorbar(scat_pca, ax=axs, orientation="horizontal")
fig.suptitle("Multiclass-Multilabel PCovC")

# %%
Loading