1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-26 13:49:14 +08:00
golearn/pca/pca.go

98 lines
2.2 KiB
Go
Raw Normal View History

2015-03-12 02:51:31 +05:00
//Implementation of Principal Component Analysis(PCA) with SVD
package pca
import (
"gonum.org/v1/gonum/mat"
2015-03-12 02:51:31 +05:00
)
type PCA struct {
Num_components int
svd *mat.SVD
2015-03-12 02:51:31 +05:00
}
// Number of components. 0 - by default, use number of features as number of components
func NewPCA(num_components int) *PCA {
return &PCA{Num_components: num_components}
2015-03-12 02:51:31 +05:00
}
2018-02-27 11:39:57 +02:00
// Fit PCA model and transform data
// Need return is base.FixedDataGrid
func (pca *PCA) FitTransform(X *mat.Dense) *mat.Dense {
2018-02-27 11:39:57 +02:00
return pca.Fit(X).Transform(X)
}
2015-03-12 02:51:31 +05:00
2018-02-27 11:39:57 +02:00
// Fit PCA model
func (pca *PCA) Fit(X *mat.Dense) *PCA {
2018-02-27 11:39:57 +02:00
// Mean to input data
2015-03-12 02:51:31 +05:00
M := mean(X)
X = matrixSubVector(X, M)
2018-02-27 11:39:57 +02:00
// Get SVD decomposition from data
pca.svd = &mat.SVD{}
ok := pca.svd.Factorize(X, mat.SVDThin)
if !ok {
panic("Unable to factorize")
}
2015-03-12 02:51:31 +05:00
if pca.Num_components < 0 {
panic("Number of components can't be less than zero")
}
2018-02-27 11:39:57 +02:00
return pca
}
// Need return is base.FixedDataGrid
func (pca *PCA) Transform(X *mat.Dense) *mat.Dense {
2018-02-27 11:39:57 +02:00
if pca.svd == nil {
panic("You should to fit PCA model first")
}
num_samples, num_features := X.Dims()
vTemp := new(mat.Dense)
pca.svd.VTo(vTemp)
2015-03-12 02:51:31 +05:00
//Compute to full data
if pca.Num_components == 0 || pca.Num_components > num_features {
return compute(X, vTemp)
2015-03-12 02:51:31 +05:00
}
X = compute(X, vTemp)
result := mat.NewDense(num_samples, pca.Num_components, nil)
result.Copy(X)
2015-03-12 02:51:31 +05:00
return result
}
//Helpful private functions
//Compute mean of the columns of input matrix
func mean(matrix *mat.Dense) *mat.Dense {
2015-03-12 02:51:31 +05:00
rows, cols := matrix.Dims()
meanVector := make([]float64, cols)
for i := 0; i < cols; i++ {
sum := mat.Sum(matrix.ColView(i))
meanVector[i] = sum / float64(rows)
2015-03-12 02:51:31 +05:00
}
return mat.NewDense(1, cols, meanVector)
2015-03-12 02:51:31 +05:00
}
// After computing of mean, compute: X(input matrix) - X(mean vector)
func matrixSubVector(mat, vec *mat.Dense) *mat.Dense {
2015-03-12 02:51:31 +05:00
rowsm, colsm := mat.Dims()
_, colsv := vec.Dims()
if colsv != colsm {
panic("Error in dimension")
}
for i := 0; i < rowsm; i++ {
for j := 0; j < colsm; j++ {
mat.Set(i, j, (mat.At(i, j) - vec.At(0, j)))
2015-03-12 02:51:31 +05:00
}
}
return mat
}
//Multiplication of X(input data) and V(from SVD)
func compute(X, Y mat.Matrix) *mat.Dense {
var ret mat.Dense
ret.Mul(X, Y)
return &ret
}