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 ¶
- func NormalizeTransformed(vector []float64) []float64
- type PCA
- func (p *PCA) InverseTransform(reduced []float64) ([]float64, error)
- func (p *PCA) ReconstructionError(vectors [][]float64) (float64, error)
- func (p *PCA) Save(w io.Writer) error
- func (p *PCA) TotalVarianceExplained() float64
- func (p *PCA) Transform(vector []float64) ([]float64, error)
- func (p *PCA) TransformBatch(vectors [][]float64) ([][]float64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NormalizeTransformed ¶
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 ¶
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 (*PCA) InverseTransform ¶
InverseTransform maps a reduced vector back to the original space (approximate). Useful for debugging and visualization.
func (*PCA) ReconstructionError ¶
ReconstructionError computes the mean squared reconstruction error for given vectors. Lower values indicate better preservation of information.
func (*PCA) TotalVarianceExplained ¶
TotalVarianceExplained returns the cumulative variance explained by all components.