mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
Make manhattan support both col&row vectors.
This commit is contained in:
parent
b045cf83a5
commit
9a61332551
@ -8,11 +8,12 @@ import (
|
||||
)
|
||||
|
||||
func TestEuclidean(t *testing.T) {
|
||||
var vectorX, vectorY *mat64.Dense
|
||||
euclidean := NewEuclidean()
|
||||
|
||||
Convey("Given two vectors", t, func() {
|
||||
vectorX := mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY := mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
vectorX = mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY = mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
|
||||
Convey("When doing inner product", func() {
|
||||
result := euclidean.InnerProduct(vectorX, vectorY)
|
||||
|
@ -13,13 +13,24 @@ func NewManhattan() *Manhattan {
|
||||
}
|
||||
|
||||
func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
var length int
|
||||
subVector := mat64.NewDense(0, 0, nil)
|
||||
subVector.Sub(vectorX, vectorY)
|
||||
|
||||
r, _ := subVector.Dims()
|
||||
r, c := subVector.Dims()
|
||||
|
||||
if r == 1 {
|
||||
// Force transpose to column vector
|
||||
subVector.TCopy(subVector)
|
||||
length = c
|
||||
} else if c == 1 {
|
||||
length = r
|
||||
} else {
|
||||
panic(mat64.ErrShape)
|
||||
}
|
||||
|
||||
result := .0
|
||||
for i := 0; i < r; i++ {
|
||||
for i := 0; i < length; i++ {
|
||||
result += math.Abs(subVector.At(i, 0))
|
||||
}
|
||||
|
||||
|
@ -8,19 +8,34 @@ import (
|
||||
)
|
||||
|
||||
func TestManhattan(t *testing.T) {
|
||||
var vectorX, vectorY *mat64.Dense
|
||||
manhattan := NewManhattan()
|
||||
|
||||
Convey("Given two vectors", t, func() {
|
||||
vectorX := mat64.NewDense(3, 1, []float64{2, 2, 3})
|
||||
vectorY := mat64.NewDense(3, 1, []float64{1, 4, 5})
|
||||
vectorX = mat64.NewDense(3, 1, []float64{2, 2, 3})
|
||||
vectorY = mat64.NewDense(3, 1, []float64{1, 4, 5})
|
||||
|
||||
Convey("When calculating distance", func() {
|
||||
Convey("When calculating distance with column vectors", func() {
|
||||
result := manhattan.Distance(vectorX, vectorY)
|
||||
|
||||
Convey("The result should be 5", func() {
|
||||
So(result, ShouldEqual, 5)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("When calculating distance with row vectors", func() {
|
||||
vectorX.TCopy(vectorX)
|
||||
vectorY.TCopy(vectorY)
|
||||
result := manhattan.Distance(vectorX, vectorY)
|
||||
|
||||
Convey("The result should be 5", func() {
|
||||
So(result, ShouldEqual, 5)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("When calculating distance with row and column vectors", func() {
|
||||
vectorX.TCopy(vectorX)
|
||||
So(func() { manhattan.Distance(vectorX, vectorY) }, ShouldPanicWith, mat64.ErrShape)
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -8,11 +8,12 @@ import (
|
||||
)
|
||||
|
||||
func TestPolyKernel(t *testing.T) {
|
||||
var vectorX, vectorY *mat64.Dense
|
||||
polyKernel := NewPolyKernel(3)
|
||||
|
||||
Convey("Given two vectors", t, func() {
|
||||
vectorX := mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY := mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
vectorX = mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY = mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
|
||||
Convey("When doing inner product", func() {
|
||||
result := polyKernel.InnerProduct(vectorX, vectorY)
|
||||
|
@ -8,11 +8,12 @@ import (
|
||||
)
|
||||
|
||||
func TestRBFKernel(t *testing.T) {
|
||||
var vectorX, vectorY *mat64.Dense
|
||||
rbfKernel := NewRBFKernel(0.1)
|
||||
|
||||
Convey("Given two vectors", t, func() {
|
||||
vectorX := mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY := mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
vectorX = mat64.NewDense(3, 1, []float64{1, 2, 3})
|
||||
vectorY = mat64.NewDense(3, 1, []float64{2, 4, 5})
|
||||
|
||||
Convey("When doing inner product", func() {
|
||||
result := rbfKernel.InnerProduct(vectorX, vectorY)
|
||||
|
Loading…
x
Reference in New Issue
Block a user