pca

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pca provides Principal Component Analysis for dimensionality reduction.

PCA reduces high-dimensional vectors to a lower dimension while preserving the most variance. This is useful for reducing CKKS encryption/decryption latency and bandwidth, with minimal impact on search accuracy.

The PCA transform is applied client-side before encryption, so it has no impact on privacy — the server never sees original or reduced vectors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeTransformed

func NormalizeTransformed(vector []float64) []float64

NormalizeTransformed normalizes a PCA-transformed vector to unit length. This preserves the cosine similarity interpretation after dimensionality reduction.

Types

type PCA

type PCA struct {
	// Components are the top-k principal component vectors (k x originalDim).
	// Each row is a unit eigenvector of the covariance matrix.
	Components *mat.Dense

	// Mean is the per-dimension mean of the training data.
	Mean []float64

	// SingularValues holds the singular values corresponding to each component.
	SingularValues []float64

	// VarianceExplained holds the fraction of total variance explained by each component.
	VarianceExplained []float64

	// OriginalDim is the input dimension.
	OriginalDim int

	// ReducedDim is the output dimension (number of components kept).
	ReducedDim int
}

PCA holds a fitted PCA model for dimensionality reduction.

func Fit

func Fit(vectors [][]float64, targetDim int) (*PCA, error)

Fit computes a PCA model from the given training vectors. targetDim specifies how many principal components to keep. All input vectors must have the same length.

func LoadPCA

func LoadPCA(r io.Reader) (*PCA, error)

LoadPCA deserializes a PCA model from a reader.

func (*PCA) InverseTransform

func (p *PCA) InverseTransform(reduced []float64) ([]float64, error)

InverseTransform maps a reduced vector back to the original space (approximate). Useful for debugging and visualization.

func (*PCA) ReconstructionError

func (p *PCA) ReconstructionError(vectors [][]float64) (float64, error)

ReconstructionError computes the mean squared reconstruction error for given vectors. Lower values indicate better preservation of information.

func (*PCA) Save

func (p *PCA) Save(w io.Writer) error

Save serializes the PCA model to a writer using gob encoding.

func (*PCA) TotalVarianceExplained

func (p *PCA) TotalVarianceExplained() float64

TotalVarianceExplained returns the cumulative variance explained by all components.

func (*PCA) Transform

func (p *PCA) Transform(vector []float64) ([]float64, error)

Transform projects a single vector into the reduced PCA space. Input vector must have length OriginalDim. Output has length ReducedDim.

func (*PCA) TransformBatch

func (p *PCA) TransformBatch(vectors [][]float64) ([][]float64, error)

TransformBatch projects multiple vectors into the reduced PCA space.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL