Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
stat: trim wide backing vecs matrix during PCA
Browse files Browse the repository at this point in the history
vecs is wide when a is wide, but vectors beyond n are not valid, so
clone the result view into vecs. This costs an allocation when it
happens, but potentially saves significant space - when n << d.
  • Loading branch information
kortschak committed Mar 6, 2016
1 parent 91c2813 commit aa29fc4
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions pca.go
Expand Up @@ -57,9 +57,12 @@ func PrincipalComponents(a mat64.Matrix, weights []float64) (vecs *mat64.Dense,
return nil, nil, false
}

var v mat64.Dense
v.VFromSVD(&svd)
vecs = v.View(0, 0, d, min(n, d)).(*mat64.Dense)
vecs = &mat64.Dense{}
vecs.VFromSVD(&svd)
if n < d {
// Don't retain columns that are not valid direction vectors.
vecs.Clone(vecs.View(0, 0, d, n))
}
vars = svd.Values(nil)
var f float64
if weights == nil {
Expand All @@ -72,10 +75,3 @@ func PrincipalComponents(a mat64.Matrix, weights []float64) (vecs *mat64.Dense,
}
return vecs, vars, true
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

0 comments on commit aa29fc4

Please sign in to comment.