1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-25 13:48:49 +08:00
This commit is contained in:
frozenkp 2018-06-07 01:55:40 +08:00 committed by ss8651twtw
parent 1b9e563772
commit 6b3f7ddab2
20 changed files with 177 additions and 177 deletions

View File

@ -71,42 +71,42 @@ func Test(t *testing.T) {
})
Convey("m[1] size diff", t, func() {
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 3}
m1[0] = []int{4, 5}
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 3}
m1[0] = []int{4, 5}
m2 := ClusterMap(make(map[int][]int))
m2[1] = []int{1, 2, 3}
m1[0] = []int{4, 5}
m2 := ClusterMap(make(map[int][]int))
m2[1] = []int{1, 2, 3}
m1[0] = []int{4, 5}
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
Convey("m[1] duplicate", t, func() {
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 1}
m1[0] = []int{4, 5}
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 1}
m1[0] = []int{4, 5}
m2 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 1}
m1[0] = []int{4, 5}
m2 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 1}
m1[0] = []int{4, 5}
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
Convey("m[0] duplicate", t, func() {
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 2}
m1[0] = []int{4, 4}
m1 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 2}
m1[0] = []int{4, 4}
m2 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 2}
m1[0] = []int{4, 4}
m2 := ClusterMap(make(map[int][]int))
m1[1] = []int{1, 2}
m1[0] = []int{4, 4}
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
_, err := m1.Equals(m2)
So(err, ShouldNotBeNil)
})
}

View File

@ -1,9 +1,9 @@
package clustering
import (
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/metrics/pairwise"
"gonum.org/v1/gonum/mat"
"math/big"
)

View File

@ -2,10 +2,10 @@ package clustering
import (
"bufio"
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/metrics/pairwise"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
"math"
"math/big"
"os"

View File

@ -10,25 +10,25 @@ func TestExpectationMaximization(t *testing.T) {
Convey("Doing EM-based clustering", t, func() {
em, _ := NewExpectationMaximization(2)
// Initialization tests
// Trying to create NewExpectationMaximization with < 1 component
Convey("With less than one component", func() {
Convey("Creating a new instance", func () {
_, err := NewExpectationMaximization(0)
// Initialization tests
// Trying to create NewExpectationMaximization with < 1 component
Convey("With less than one component", func() {
Convey("Creating a new instance", func() {
_, err := NewExpectationMaximization(0)
Convey("Should result in a InsufficientComponentsError", func() {
So(err, ShouldEqual, InsufficientComponentsError)
})
})
})
})
})
// Data tests
// Trying to Fit with fewer samples than components
// Data tests
// Trying to Fit with fewer samples than components
Convey("With insufficient training data", func() {
Convey("Fitting", func() {
testData, err := base.ParseCSVToInstances("./gaussian_mixture_single_obs.csv", false)
So(err, ShouldBeNil)
err = em.Fit(testData)
err = em.Fit(testData)
Convey("Should result in a InsufficientDataError", func() {
So(err, ShouldEqual, InsufficientDataError)
@ -36,7 +36,7 @@ func TestExpectationMaximization(t *testing.T) {
})
})
// Trying to Predict before having Fit
// Trying to Predict before having Fit
Convey("With no training data", func() {
Convey("Predicting", func() {
testData, err := base.ParseCSVToInstances("./gaussian_mixture.csv", false)
@ -50,8 +50,8 @@ func TestExpectationMaximization(t *testing.T) {
})
})
// Computation tests
// Test the predictions are resonable
// Computation tests
// Test the predictions are resonable
Convey("With sufficient training data", func() {
instances, err := base.ParseCSVToInstances("./gaussian_mixture.csv", true)
So(err, ShouldBeNil)
@ -60,10 +60,10 @@ func TestExpectationMaximization(t *testing.T) {
err := em.Fit(instances)
So(err, ShouldBeNil)
first_mean := em.Params.Means.At(0,0)
first_mean := em.Params.Means.At(0, 0)
Convey("It converges to reasonable a value", func() {
So(first_mean, ShouldAlmostEqual, -5.973, .1)
So(first_mean, ShouldAlmostEqual, -5.973, .1)
})
})
})
@ -76,9 +76,9 @@ func BenchmarkExpectationMaximizationOneRow(b *testing.B) {
testData, _ := base.ParseCSVToInstances("./gaussian_mixture.csv", false)
em, err := NewExpectationMaximization(2)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
em.Fit(trainData)
b.ResetTimer()

View File

@ -8,25 +8,25 @@ import (
func TestHeap(t *testing.T) {
Convey("Given a heap", t, func() {
h := newHeap()
h := newHeap()
Convey("When heap is empty", func() {
Convey("The size should be 0", func() {
size := h.size()
size := h.size()
So(size, ShouldEqual, 0)
})
Convey("The maximum node should be empty", func(){
max := h.maximum()
So(max.value, ShouldBeEmpty)
So(max.length, ShouldEqual, 0)
So(max.srcRowNo, ShouldEqual, 0)
})
Convey("The maximum node should be empty", func() {
max := h.maximum()
So(max.value, ShouldBeEmpty)
So(max.length, ShouldEqual, 0)
So(max.srcRowNo, ShouldEqual, 0)
})
Convey("Extrace Max should be fine", func(){
So(func(){h.extractMax()}, ShouldNotPanic)
})
Convey("Extrace Max should be fine", func() {
So(func() { h.extractMax() }, ShouldNotPanic)
})
})
Convey("When insert 10 nodes", func() {

View File

@ -2,13 +2,13 @@ package kdtree
import (
"errors"
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/metrics/pairwise"
"gonum.org/v1/gonum/mat"
"sort"
)
type node struct {
feature int // -1: leaf, -2: nil
feature int // -1: leaf, -2: nil
value []float64
srcRowNo int
left *node

View File

@ -8,123 +8,123 @@ import (
)
func TestKdtree(t *testing.T) {
Convey("Test Build", t, func(){
Convey("When no input data", func(){
kd := New()
data := [][]float64{}
err := kd.Build(data)
So(err.Error(), ShouldEqual, "no input data")
})
Convey("Test Build", t, func() {
Convey("When no input data", func() {
kd := New()
data := [][]float64{}
err := kd.Build(data)
So(err.Error(), ShouldEqual, "no input data")
})
Convey("When amounts of features not the same", func(){
kd := New()
data := [][]float64{{3, 5}, {6, 7, 10}}
err := kd.Build(data)
So(err.Error(), ShouldEqual, "amounts of features are not the same")
})
Convey("When amounts of features not the same", func() {
kd := New()
data := [][]float64{{3, 5}, {6, 7, 10}}
err := kd.Build(data)
So(err.Error(), ShouldEqual, "amounts of features are not the same")
})
Convey("When only one data", func(){
kd := New()
data := [][]float64{{3, 5}}
err := kd.Build(data)
So(err, ShouldBeNil)
})
Convey("When only one data", func() {
kd := New()
data := [][]float64{{3, 5}}
err := kd.Build(data)
So(err, ShouldBeNil)
})
Convey("When data all the same", func(){
kd := New()
data := [][]float64{{3, 5}, {3, 5}, {3, 5}}
err := kd.Build(data)
So(err, ShouldBeNil)
})
})
Convey("When data all the same", func() {
kd := New()
data := [][]float64{{3, 5}, {3, 5}, {3, 5}}
err := kd.Build(data)
So(err, ShouldBeNil)
})
})
Convey("Test Search", t, func() {
Convey("Functionally test", func(){
kd := New()
data := [][]float64{{2, 3}, {5, 4}, {4, 7}, {8, 1}, {7, 2}, {9, 6}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
Convey("Functionally test", func() {
kd := New()
data := [][]float64{{2, 3}, {5, 4}, {4, 7}, {8, 1}, {7, 2}, {9, 6}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
Convey("When k is 3 with euclidean", func() {
result, _, _ := kd.Search(3, euclidean, []float64{7, 3})
Convey("When k is 3 with euclidean", func() {
result, _, _ := kd.Search(3, euclidean, []float64{7, 3})
Convey("The result[0] should be 4", func() {
So(result[0], ShouldEqual, 4)
})
Convey("The result[1] should be 3", func() {
So(result[1], ShouldEqual, 3)
})
Convey("The result[2] should be 1", func() {
So(result[2], ShouldEqual, 1)
})
})
Convey("The result[0] should be 4", func() {
So(result[0], ShouldEqual, 4)
})
Convey("The result[1] should be 3", func() {
So(result[1], ShouldEqual, 3)
})
Convey("The result[2] should be 1", func() {
So(result[2], ShouldEqual, 1)
})
})
Convey("When k is 2 with euclidean", func() {
result, _, _ := kd.Search(2, euclidean, []float64{7, 3})
Convey("When k is 2 with euclidean", func() {
result, _, _ := kd.Search(2, euclidean, []float64{7, 3})
Convey("The result[0] should be 4", func() {
So(result[0], ShouldEqual, 4)
})
Convey("The result[1] should be 1", func() {
So(result[1], ShouldEqual, 1)
})
})
})
Convey("The result[0] should be 4", func() {
So(result[0], ShouldEqual, 4)
})
Convey("The result[1] should be 1", func() {
So(result[1], ShouldEqual, 1)
})
})
})
Convey("When k is larger than amount of trainData", func(){
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(3, euclidean, []float64{7, 3})
So(err.Error(), ShouldEqual, "k is largerer than amount of trainData")
})
Convey("When k is larger than amount of trainData", func() {
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(3, euclidean, []float64{7, 3})
So(err.Error(), ShouldEqual, "k is largerer than amount of trainData")
})
Convey("When features of target is larger than trainData", func(){
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(1, euclidean, []float64{7, 3, 5})
So(err.Error(), ShouldEqual, "amount of features is not equal")
})
Convey("When features of target is larger than trainData", func() {
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(1, euclidean, []float64{7, 3, 5})
So(err.Error(), ShouldEqual, "amount of features is not equal")
})
Convey("When node.feature is -2", func(){
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(1, euclidean, []float64{7, 3})
So(err, ShouldBeNil)
})
Convey("When node.feature is -2", func() {
kd := New()
data := [][]float64{{3, 5}, {2, 1}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
_, _, err := kd.Search(1, euclidean, []float64{7, 3})
So(err, ShouldBeNil)
})
Convey("Search All Node (left)", func(){
kd := New()
data := [][]float64{{1, 2}, {5, 6}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{7, 3})
So(result[0], ShouldEqual, 1)
})
Convey("Search All Node (left)", func() {
kd := New()
data := [][]float64{{1, 2}, {5, 6}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{7, 3})
So(result[0], ShouldEqual, 1)
})
Convey("Search when node length larger than heap max", func(){
Convey("Search All Node (left)", func(){
kd := New()
data := [][]float64{{1, 2}, {5, 6}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{8, 7})
So(result[0], ShouldEqual, 2)
})
Convey("Search when node length larger than heap max", func() {
Convey("Search All Node (left)", func() {
kd := New()
data := [][]float64{{1, 2}, {5, 6}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{8, 7})
So(result[0], ShouldEqual, 2)
})
Convey("Search All Node (right)", func(){
kd := New()
data := [][]float64{{1, 2}, {5, 4}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{3, 3})
So(result[0], ShouldEqual, 0)
})
})
Convey("Search All Node (right)", func() {
kd := New()
data := [][]float64{{1, 2}, {5, 4}, {9, 10}}
kd.Build(data)
euclidean := pairwise.NewEuclidean()
result, _, _ := kd.Search(1, euclidean, []float64{3, 3})
So(result[0], ShouldEqual, 0)
})
})
})
}

View File

@ -8,11 +8,11 @@ import (
"fmt"
"github.com/gonum/matrix"
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/kdtree"
"github.com/sjwhitworth/golearn/metrics/pairwise"
"github.com/sjwhitworth/golearn/utilities"
"gonum.org/v1/gonum/mat"
)
// A KNNClassifier consists of a data matrix, associated labels in the same order as the matrix, searching algorithm, and a distance function.

View File

@ -253,7 +253,7 @@ func (b *BaggedModel) SaveWithPrefix(writer *base.ClassifierSerializer, prefix s
// Save the classifiers
for i, c := range b.Models {
clsPrefix := fmt.Sprintf("%s/", pI( "CLASSIFIERS", i))
clsPrefix := fmt.Sprintf("%s/", pI("CLASSIFIERS", i))
err = c.SaveWithPrefix(writer, clsPrefix)
if err != nil {
return base.FormatError(err, "Can't save classifier %d", i)
@ -308,7 +308,7 @@ func (b *BaggedModel) LoadWithPrefix(reader *base.ClassifierDeserializer, prefix
// Reload the classifiers
for i, m := range b.Models {
clsPrefix := fmt.Sprintf("%s/", pI( "CLASSIFIERS", i))
clsPrefix := fmt.Sprintf("%s/", pI("CLASSIFIERS", i))
err := m.LoadWithPrefix(reader, clsPrefix)
if err != nil {
return base.DescribeError("Can't read classifier", err)

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestChebyshev(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestCosine(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestCranberrra(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestEuclidean(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestManhattan(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestPolyKernel(t *testing.T) {

View File

@ -3,8 +3,8 @@ package pairwise
import (
"testing"
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestRBFKernel(t *testing.T) {

View File

@ -2,9 +2,9 @@ package neural
import (
"fmt"
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/filters"
"gonum.org/v1/gonum/mat"
"math"
"math/rand"
)

View File

@ -1,9 +1,9 @@
package neural
import (
"gonum.org/v1/gonum/mat"
"github.com/sjwhitworth/golearn/base"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
"testing"
)

View File

@ -12,7 +12,7 @@ import (
// NeuralFunction.
type Network struct {
origWeights *mat.Dense
weights *mat.Dense // n * n
weights *mat.Dense // n * n
biases []float64 // n for each neuron
funcs []NeuralFunction // for each neuron
size int

View File

@ -1,8 +1,8 @@
package neural
import (
"gonum.org/v1/gonum/mat"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
"testing"
)