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:
parent
b473ff6881
commit
6211206b73
@ -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 ||
|
||||||
|
33
metrics/pairwise/rbf_kernel.go
Normal file
33
metrics/pairwise/rbf_kernel.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user