.ipynb

Use Non-PCA Reducers#

Use KernelPCA or other reducers with ktch plot functions via explicit override parameters.

The reducer convenience parameter targets PCA-compatible estimators. For reducers with different attribute names (e.g., KernelPCA stores eigenvalues in eigenvalues_ instead of explained_variance_), pass the override parameters directly.

Setup#

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import KernelPCA

from ktch.datasets import load_outline_mosquito_wings
from ktch.harmonic import EllipticFourierAnalysis
from ktch.plot import shape_variation_plot, morphospace_plot

data = load_outline_mosquito_wings(as_frame=True)
coords = data.coords.to_numpy().reshape(-1, 100, 2)

efa = EllipticFourierAnalysis(n_harmonics=20)
coef = efa.fit_transform(coords)

kpca = KernelPCA(n_components=5, kernel="rbf", fit_inverse_transform=True)
kpca.fit(coef)
KernelPCA(fit_inverse_transform=True, kernel='rbf', n_components=5)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Shape variation with KernelPCA#

fig = shape_variation_plot(
    reducer_inverse_transform=kpca.inverse_transform,
    explained_variance=kpca.eigenvalues_,
    n_components=kpca.n_components,
    descriptor=efa,
    components=(0, 1, 2),
)
../../_images/b06cd6c2ff716a8a3e68885b1a22257fe4097236a66dcacc5d6e329ca53b24b1.png

Morphospace with KernelPCA#

scores = kpca.transform(coef)
df_kpca = pd.DataFrame(
    scores[:, :2], columns=["KPC1", "KPC2"],
)
df_kpca.index = data.meta.index
df_kpca = df_kpca.join(data.meta)

ax = morphospace_plot(
    data=df_kpca,
    x="KPC1", y="KPC2", hue="genus",
    reducer_inverse_transform=kpca.inverse_transform,
    n_components=kpca.n_components,
    descriptor=efa,
    palette="Paired",
    n_shapes=5,
)
../../_images/7b7006910959236bb562490b8794daf0fbf15a9416edc97d3cfc83dc55fb7365.png

See also