func TestClassifyAssignsHighestScoredClassToFeature(t *testing.T) { classSize, dimensions := 4, 8 test := classificationTest{ feature: dense.New(1, 8)(0, 1, 0.5, -1, 0, 0, 2, 0), class: 2, } weights := dense.New(classSize, dimensions)( 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 1, 0, 0, -1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, ) c := New(classSize, dimensions).Update(weights) if class := c.Classify(test.feature); class != test.class { t.Fatalf( "Classifier should assign %d to the feature, but %d is assigned.", test.class, class, ) } }
func TestPerceptronLearnsClassification(t *testing.T) { classSize, dimensions := 2, 6 iterations := 3 feature := dense.New(1, dimensions) instances := []*olive.Instance{ olive.NewInstance(feature(1, 1, 1, 0, 0, 0), 0), olive.NewInstance(feature(1, 1, 0, 0, 0, 0), 0), olive.NewInstance(feature(0, 0, 0, 1, 1, 1), 1), olive.NewInstance(feature(0, 0, 0, 0, 1, 1), 1), } c := New(iterations).Learn(classifier.New(classSize, dimensions), instances) for index, instance := range instances { class := c.Classify(instance.Feature()) if class == instance.Class() { continue } t.Fatalf( "The instance %d should be classified into %d, but is classified into %d", index, instance.Class(), class, ) } }
func TestScalarMultiplyIsCommutative(t *testing.T) { m := dense.New(2, 2)( 0, 1, 2, 3, ) n := dense.New(2, 2)( 0, 1, 2, 3, ) s := 2.0 if Scalar(s).Multiply(m).Equal(n.Scalar(s)) { return } t.Fatal("Scalar multiplication should be commutative.") }
func TestIsNotDiagonalNonSquareMutableDense(t *testing.T) { m := dense.New(4, 3)( 2, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, ) if !IsDiagonal(m) { return } t.Fatal("This matrix should not be diagonal.") }
func TestIsNotDiagonalMutableDense(t *testing.T) { m := dense.New(4, 4)( 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, ) if !IsDiagonal(m) { return } t.Fatal("This matrix should not be diagonal.") }
func TestIsZerosMutableDense(t *testing.T) { m := dense.New(4, 3)( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ) if IsZeros(m) { return } t.Fatal("This matrix should be zeros.") }
func TestIsDiagonalMutableDense(t *testing.T) { m := dense.New(4, 4)( 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, ) if IsDiagonal(m) { return } t.Fatal("This matrix should be diagonal.") }
func TestIsNotSquareMutableDense(t *testing.T) { m := dense.New(4, 3)( 0, 1, 2, 4, 5, 0, 2, 3, 4, 0, 1, 2, ) if !IsSquare(m) { return } t.Fatal("This matrix should not be square.") }
func TestIsSquareMutableDense(t *testing.T) { m := dense.New(4, 4)( 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, ) if IsSquare(m) { return } t.Fatal("This matrix should be square.") }
func TestIsNotZerosMutableDense(t *testing.T) { m := dense.New(4, 3)( 0, 1, 2, 4, 5, 0, 2, 3, 4, 0, 1, 2, ) if !IsZeros(m) { return } t.Fatal("This matrix should not be zeros.") }
func TestIsNotScalarNonSquareMutableDense(t *testing.T) { m := dense.New(4, 3)( 7, 0, 0, 0, 7, 0, 0, 0, 7, 0, 0, 0, ) if !IsScalar(m) { return } t.Fatal("This matrix should not be scalar.") }
func TestIsScalarMutableDense(t *testing.T) { m := dense.New(4, 4)( 7, 0, 0, 0, 0, 7, 0, 0, 0, 0, 7, 0, 0, 0, 0, 7, ) if IsScalar(m) { return } t.Fatal("This matrix should be scalar.") }
func TestIsNotIdentityNonSquareMutableDense(t *testing.T) { m := dense.New(4, 3)( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, ) if !IsIdentity(m) { return } t.Fatal("This matrix should not be identity.") }
func TestIsIdentityMutableDense(t *testing.T) { m := dense.New(4, 4)( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, ) if IsIdentity(m) { return } t.Fatal("This matrix should be identity.") }