Exemple #1
0
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,
		)
	}
}
Exemple #2
0
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,
		)
	}
}
Exemple #3
0
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.")
}
Exemple #4
0
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.")
}
Exemple #5
0
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.")
}
Exemple #6
0
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.")
}
Exemple #7
0
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.")
}
Exemple #8
0
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.")
}
Exemple #9
0
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.")
}
Exemple #10
0
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.")
}
Exemple #11
0
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.")
}
Exemple #12
0
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.")
}
Exemple #13
0
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.")
}
Exemple #14
0
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.")
}