← Back to list

Candecomp-PARAFAC decomposition

level: ควรรู้ multilinear algebra ก่อน

Peratham Wiriyathammabhum · 2026-02-07 11:41 · 5 claps · 2.1 min read
#candecomp-parafac #tensor-decomposition #tensorly #multilinear-algebra #python
Open on Medium ↗
Wiki topics: VIS · Visual & Graphic Design 📐 · Mathematics

Candecomp-PARAFAC decomposition

level: ควรรู้ multilinear algebra ก่อน

Candecomp-PARAFAC decomposition เป็น tensor decomposaition to polyadicform แบบนึง โดย Carroll and Chang, 1970 หรือ Harshman, 1970 สมการคือ polyadic

[2]

[2]

โดยแยกตัวประกอบ แบบวนซ้ำ iterative

[2]

[2]

มี Lambda จากรูปที่พิสูจน์โดย Kruskal 1977

[2]

[2]

ตัวอย่าง COVID-19 Serology Dataset Analysis with CP [1]

import numpy as np
import tensorly as tl
from tensorly.decomposition import parafac
from tensorly.datasets.data_imports import load_covid19_serology
from matplotlib import pyplot as plt
from matplotlib.cm import ScalarMappable

data = load_covid19_serology()
comps = np.arange(1, 7)
CMTFfacs = [
    parafac(
        data.tensor, cc, tol=1e-10, n_iter_max=1000, linesearch=True, orthogonalise=2
    )
    for cc in comps
]
def reconstructed_variance(tFac, tIn=None):
    """This function calculates the amount of variance captured (R2X) by the tensor method."""
    tMask = np.isfinite(tIn)
    vTop = np.sum(np.square(tl.cp_to_tensor(tFac) * tMask - np.nan_to_num(tIn)))
    vBottom = np.sum(np.square(np.nan_to_num(tIn)))
    return 1.0 - vTop / vBottom

fig1 = plt.figure()
CMTFR2X = np.array([reconstructed_variance(f, data.tensor) for f in CMTFfacs])
plt.plot(comps, CMTFR2X, "bo")
plt.xlabel("Number of Components")
plt.ylabel("Variance Explained (R2X)")
plt.gca().set_xlim([0.0, np.amax(comps) + 0.5])
plt.gca().set_ylim([0, 1])

[1]

[1]

tfac = CMTFfacs[1]

# Ensure that factors are negative on at most one direction.
tfac.factors[1][:, 0] *= -1
tfac.factors[2][:, 0] *= -1

fig2, ax = plt.subplots(1, 3, figsize=(16, 6))
for ii in [0, 1, 2]:
    fac = tfac.factors[ii]
    scales = np.linalg.norm(fac, ord=np.inf, axis=0)
    fac /= scales

    ax[ii].imshow(fac, cmap="PiYG", vmin=-1, vmax=1)
    ax[ii].set_xticks([0, 1])
    ax[ii].set_xticklabels(["Comp. 1", "Comp. 2"])
    ax[ii].set_yticks(range(len(data.ticks[ii])))
    if ii == 0:
        ax[0].set_yticklabels(
            [
                (
                    data.ticks[0][i]
                    if i == 0 or data.ticks[0][i] != data.ticks[0][i - 1]
                    else ""
                )
                for i in range(len(data.ticks[0]))
            ]
        )
    else:
        ax[ii].set_yticklabels(data.ticks[ii])
    ax[ii].set_title(data.dims[ii])
    ax[ii].set_aspect("auto")

fig2.colorbar(ScalarMappable(norm=plt.Normalize(-1, 1), cmap="PiYG"))

From the results, we can see that serum COVID-19 immunity separates into two distinct signals, represented by two CP components: a clear acute response with IgG3, IgM, and IgA, and a long-term, IgG1-specific response. Samples from patients with different symptoms can be distinguished from these two components. This indicates that CP decomposition is a great tool to find these biologically significant signals. [1]

From the results, we can see that serum COVID-19 immunity separates into two distinct signals, represented by two CP components: a clear acute response with IgG3, IgM, and IgA, and a long-term, IgG1-specific response. Samples from patients with different symptoms can be distinguished from these two components. This indicates that CP decomposition is a great tool to find these biologically significant signals. [1]

References

[1] Jean Kossaifi, Yannis Panagakis, Anima Anandkumar and Maja Pantic, TensorLy: Tensor Learning in Python, Journal of Machine Learning Research, Year: 2019, Volume: 20, Issue: 26, Pages: 1−6. http://jmlr.org/papers/v20/18-277.html. https://tensorly.org/stable/index.html

[2] Kolda, Tamara G., and Brett W. Bader. “Tensor decompositions and applications.” SIAM review 51.3 (2009): 455–500. https://www.kolda.net/publication/TensorReview.pdf

ตั้งแต่มีนา 2563 มีคนจาก Stanford อัพโค้ด RNA ทั้งยวง วัคซีน mRNA Pfizer กับ ModeRNA assemblies

ใน GitHub https://gizmodo.com/stanford-scientists-post-entire-mrna-sequence-for-moder-1846576268 แน่นอนว่า ทุกวันนี้ การค้นหาสูตรยา หรือสร้างสูตรยา in silico ทำได้ในคอมกับตามบ้านง่ายๆได้


메타데이터
post_id
274518434d3d
slug
candecomp-parafac-decomposition-274518434d3d
url
https://medium.com/@peratham/candecomp-parafac-decomposition-274518434d3d
canonical_url
https://medium.com/@peratham/candecomp-parafac-decomposition-274518434d3d
author_url
https://medium.com/@peratham
status
ok
fetched_at
2026-08-11 10:30:20