mirror of
https://github.com/gonum/gonum.git
synced 2025-10-29 01:33:14 +08:00
stat: trim wide backing vecs matrix during PCA
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.
This commit is contained in:
16
pca.go
16
pca.go
@@ -57,9 +57,12 @@ func PrincipalComponents(a mat64.Matrix, weights []float64) (vecs *mat64.Dense,
|
|||||||
return nil, nil, false
|
return nil, nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
var v mat64.Dense
|
vecs = &mat64.Dense{}
|
||||||
v.VFromSVD(&svd)
|
vecs.VFromSVD(&svd)
|
||||||
vecs = v.View(0, 0, d, min(n, d)).(*mat64.Dense)
|
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)
|
vars = svd.Values(nil)
|
||||||
var f float64
|
var f float64
|
||||||
if weights == nil {
|
if weights == nil {
|
||||||
@@ -72,10 +75,3 @@ func PrincipalComponents(a mat64.Matrix, weights []float64) (vecs *mat64.Dense,
|
|||||||
}
|
}
|
||||||
return vecs, vars, true
|
return vecs, vars, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user