Skip to content

Commit

Permalink
Merge pull request #233 from Sentimentron/fixes-jun-19
Browse files Browse the repository at this point in the history
Fix #232 and #229
  • Loading branch information
Sentimentron authored Jun 19, 2019
2 parents 82e59c8 + 850c914 commit c3cae57
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
22 changes: 10 additions & 12 deletions clustering/em.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ExpectationMaximization struct {

type Params struct {
Means *mat.Dense
Covs []mat.Symmetric
Covs []*mat.SymDense
}

// Number of Gaussians to fit in the mixture
Expand Down Expand Up @@ -131,27 +131,25 @@ func maximization(X *mat.Dense, y mat.Vector, p Params, n_comps int) Params {
// Initialize the new parameters
var p_new Params
p_new.Means = mat.NewDense(n_comps, n_feats, nil)
p_new.Covs = make([]mat.Symmetric, n_comps)
p_new.Covs = make([]*mat.SymDense, n_comps)

//Update the parameters
// Update the parameters
for k := 0; k < n_comps; k++ {
X_yk := where(X, y, k)
n_obs, _ := X_yk.Dims()
var covs_k mat.Symmetric
covs_k_reg := mat.NewSymDense(n_feats, nil)

if n_obs <= 1 {
p_new.Means.SetRow(k, p.Means.RawRowView(k))
covs_k = p.Covs[k]
covs_k_reg = p.Covs[k]
} else if n_obs < n_feats {
p_new.Means.SetRow(k, means(X_yk))
covs_k = shrunkCovariance(X_yk)
covs_k_reg = shrunkCovariance(X_yk)
} else {
p_new.Means.SetRow(k, means(X_yk))
covs_k = stat.CovarianceMatrix(nil, X_yk, nil)
stat.CovarianceMatrix(covs_k_reg, X_yk, nil)
}
reg := mat.NewSymDense(n_feats, symVals(n_feats, 0.000001))
covs_k_reg := mat.NewSymDense(n_feats, nil)
covs_k_reg.AddSym(covs_k, reg)

p_new.Covs[k] = covs_k_reg
}

Expand Down Expand Up @@ -228,8 +226,8 @@ func initMeans(X *mat.Dense, n_comps, n_feats int) *mat.Dense {
}

// Creates a n_comps array of n_feats x n_feats mat.Symmetrics
func initCovariance(n_comps, n_feats int) []mat.Symmetric {
var result []mat.Symmetric
func initCovariance(n_comps, n_feats int) []*mat.SymDense {
var result []*mat.SymDense
floats := identity(n_feats)
for k := 0; k < n_comps; k++ {
matrix := mat.NewSymDense(n_feats, floats)
Expand Down
2 changes: 1 addition & 1 deletion linear_models/linear_regression.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (lr *LinearRegression) Fit(inst base.FixedDataGrid) error {
qr.RTo(&reg)

var transposed, qty mat.Dense
transposed.Clone(q.T())
transposed.CloneFrom(q.T())
qty.Mul(&transposed, observed)

regressionCoefficients := make([]float64, n)
Expand Down
2 changes: 1 addition & 1 deletion metrics/pairwise/chebyshev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestChebyshev(t *testing.T) {
})

Convey("When calculating distance with different dimension matrices", func() {
vectorX.Clone(vectorX.T())
vectorX.CloneFrom(vectorX.T())
So(func() { chebyshev.Distance(vectorX, vectorY) }, ShouldPanic)
})

Expand Down
2 changes: 1 addition & 1 deletion metrics/pairwise/cranberra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCranberrra(t *testing.T) {
})

Convey("When calculating distance with different dimension matrices", func() {
vectorX.Clone(vectorX.T())
vectorX.CloneFrom(vectorX.T())
So(func() { cranberra.Distance(vectorX, vectorY) }, ShouldPanic)
})

Expand Down
2 changes: 1 addition & 1 deletion metrics/pairwise/manhattan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestManhattan(t *testing.T) {
})

Convey("When calculating distance with different dimension matrices", func() {
vectorX.Clone(vectorX.T())
vectorX.CloneFrom(vectorX.T())
So(func() { manhattan.Distance(vectorX, vectorY) }, ShouldPanic)
})

Expand Down
4 changes: 2 additions & 2 deletions neural/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (n *Network) Activate(with *mat.Dense, maxIterations int) {
}

tmp := new(mat.Dense)
tmp.Clone(with)
tmp.CloneFrom(with)

// Main loop
for i := 0; i < maxIterations; i++ {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (n *Network) Error(outArg, errArg *mat.Dense, maxIterations int) *mat.Dense

// Transpose weights matrix
reverseWeights := mat.DenseCopyOf(n.weights)
reverseWeights.Clone(n.weights.T())
reverseWeights.CloneFrom(n.weights.T())

// We only need a certain number of passes
for i := 0; i < maxIterations; i++ {
Expand Down

0 comments on commit c3cae57

Please sign in to comment.