mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +08:00
Add documents.
This commit is contained in:
parent
9a61332551
commit
1ec702a1e5
@ -12,13 +12,15 @@ func NewEuclidean() *Euclidean {
|
|||||||
return &Euclidean{}
|
return &Euclidean{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute usual inner product in the sense of euclidean.
|
||||||
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
result := vectorX.Dot(vectorY)
|
result := vectorX.Dot(vectorY)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// We may need to create Metrics / Vector interface for this
|
// Compute usual distance in the sense of euclidean.
|
||||||
|
// Also known as L2 distance.
|
||||||
func (self *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
subVector := mat64.NewDense(0, 0, nil)
|
subVector := mat64.NewDense(0, 0, nil)
|
||||||
subVector.Sub(vectorX, vectorY)
|
subVector.Sub(vectorX, vectorY)
|
||||||
|
@ -12,6 +12,8 @@ func NewManhattan() *Manhattan {
|
|||||||
return &Manhattan{}
|
return &Manhattan{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Manhattan distance, also known as L1 distance.
|
||||||
|
// Compute sum of absolute values of elements.
|
||||||
func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
var length int
|
var length int
|
||||||
subVector := mat64.NewDense(0, 0, nil)
|
subVector := mat64.NewDense(0, 0, nil)
|
||||||
|
@ -1 +1,2 @@
|
|||||||
|
// Package pairwise implements utilities to evaluate pairwise distances or inner product (via kernel).
|
||||||
package pairwise
|
package pairwise
|
||||||
|
@ -10,10 +10,13 @@ type PolyKernel struct {
|
|||||||
degree int
|
degree int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return a d-degree polynomial kernel
|
||||||
func NewPolyKernel(degree int) *PolyKernel {
|
func NewPolyKernel(degree int) *PolyKernel {
|
||||||
return &PolyKernel{degree: degree}
|
return &PolyKernel{degree: degree}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute inner product through kernel trick
|
||||||
|
// K(x, y) = (x^T y + 1)^d
|
||||||
func (self *PolyKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *PolyKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
result := vectorX.Dot(vectorY)
|
result := vectorX.Dot(vectorY)
|
||||||
result = math.Pow(result+1, float64(self.degree))
|
result = math.Pow(result+1, float64(self.degree))
|
||||||
@ -21,6 +24,7 @@ func (self *PolyKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense)
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute distance under the polynomial kernel, maybe no need.
|
||||||
func (self *PolyKernel) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *PolyKernel) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
subVector := mat64.NewDense(0, 0, nil)
|
subVector := mat64.NewDense(0, 0, nil)
|
||||||
subVector.Sub(vectorX, vectorY)
|
subVector.Sub(vectorX, vectorY)
|
||||||
|
@ -10,15 +10,18 @@ type RBFKernel struct {
|
|||||||
gamma float64
|
gamma float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Radial Basis Function Kernel
|
||||||
func NewRBFKernel(gamma float64) *RBFKernel {
|
func NewRBFKernel(gamma float64) *RBFKernel {
|
||||||
return &RBFKernel{gamma: gamma}
|
return &RBFKernel{gamma: gamma}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute inner product through kernel trick
|
||||||
|
// K(x, y) = exp(-gamma * ||x - y||^2)
|
||||||
func (self *RBFKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *RBFKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
euclidean := NewEuclidean()
|
euclidean := NewEuclidean()
|
||||||
distance := euclidean.Distance(vectorX, vectorY)
|
distance := euclidean.Distance(vectorX, vectorY)
|
||||||
|
|
||||||
result := math.Exp(self.gamma * math.Pow(distance, 2))
|
result := math.Exp(-self.gamma * math.Pow(distance, 2))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ func TestRBFKernel(t *testing.T) {
|
|||||||
Convey("When doing inner product", func() {
|
Convey("When doing inner product", func() {
|
||||||
result := rbfKernel.InnerProduct(vectorX, vectorY)
|
result := rbfKernel.InnerProduct(vectorX, vectorY)
|
||||||
|
|
||||||
Convey("The result should be 2.45960311115695", func() {
|
Convey("The result should be 0.4065696597405991", func() {
|
||||||
So(result, ShouldEqual, 2.45960311115695)
|
So(result, ShouldEqual, 0.4065696597405991)
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user