1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00

Remove Metric interface, add RBFKernel.

This commit is contained in:
Bert Chang 2014-05-02 21:10:55 +08:00
parent b473ff6881
commit 6211206b73
2 changed files with 33 additions and 5 deletions

View File

@ -4,11 +4,6 @@ import (
mat "github.com/skelterjohn/go.matrix" mat "github.com/skelterjohn/go.matrix"
) )
type Metric interface {
InnerProduct(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix)
Distance(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix)
}
func CheckDimMatch(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) bool { func CheckDimMatch(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) bool {
if vectorX.Cols() != 1 || if vectorX.Cols() != 1 ||
vectorY.Cols() != 1 || vectorY.Cols() != 1 ||

View File

@ -0,0 +1,33 @@
package pairwise
import (
"errors"
"math"
mat "github.com/skelterjohn/go.matrix"
)
type RBFKernel struct {
gamma float64
}
func NewRBFKernel(gamma float64) *RBFKernel {
return &RBFKernel{gamma: gamma}
}
func (self *RBFKernel) InnerProduct(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
if !CheckDimMatch(vectorX, vectorY) {
return 0, errors.New("Dimension mismatch")
}
euclidean := NewEuclidean()
distance, err := euclidean.Distance(vectorX, vectorY)
if err != nil {
return 0, err
}
result := math.Exp(self.gamma * math.Pow(distance, 2))
return result, nil
}