mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
receiver name should be a reflection of its identity; dont use generic names such as "me", "this", or "self"
This commit is contained in:
parent
e714470814
commit
716bddc46a
@ -12,7 +12,7 @@ func NewChebyshev() *Chebyshev {
|
||||
return &Chebyshev{}
|
||||
}
|
||||
|
||||
func (self *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (c *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
r1, c1 := vectorX.Dims()
|
||||
r2, c2 := vectorY.Dims()
|
||||
if r1 != r2 || c1 != c2 {
|
||||
|
@ -19,7 +19,7 @@ func cranberraDistanceStep(num float64, denom float64) float64 {
|
||||
return num / denom
|
||||
}
|
||||
|
||||
func (self *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (c *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
r1, c1 := vectorX.Dims()
|
||||
r2, c2 := vectorY.Dims()
|
||||
if r1 != r2 || c1 != c2 {
|
||||
|
@ -13,18 +13,18 @@ func NewEuclidean() *Euclidean {
|
||||
}
|
||||
|
||||
// InnerProduct computes a Eucledian inner product.
|
||||
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (e *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
result := vectorX.Dot(vectorY)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Distance computes Euclidean distance (also known as L2 distance).
|
||||
func (self *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (e *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
subVector := mat64.NewDense(0, 0, nil)
|
||||
subVector.Sub(vectorX, vectorY)
|
||||
|
||||
result := self.InnerProduct(subVector, subVector)
|
||||
result := e.InnerProduct(subVector, subVector)
|
||||
|
||||
return math.Sqrt(result)
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ func NewManhattan() *Manhattan {
|
||||
|
||||
// Distance computes the Manhattan distance, also known as L1 distance.
|
||||
// == the sum of the absolute values of elements.
|
||||
func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (m *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
r1, c1 := vectorX.Dims()
|
||||
r2, c2 := vectorY.Dims()
|
||||
if r1 != r2 || c1 != c2 {
|
||||
|
@ -17,18 +17,18 @@ func NewPolyKernel(degree int) *PolyKernel {
|
||||
|
||||
// InnerProduct computes the inner product through a kernel trick
|
||||
// K(x, y) = (x^T y + 1)^d
|
||||
func (self *PolyKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (p *PolyKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
result := vectorX.Dot(vectorY)
|
||||
result = math.Pow(result+1, float64(self.degree))
|
||||
result = math.Pow(result+1, float64(p.degree))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Distance computes distance under the polynomial kernel (maybe not needed?)
|
||||
func (self *PolyKernel) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (p *PolyKernel) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
subVector := mat64.NewDense(0, 0, nil)
|
||||
subVector.Sub(vectorX, vectorY)
|
||||
result := self.InnerProduct(subVector, subVector)
|
||||
result := p.InnerProduct(subVector, subVector)
|
||||
|
||||
return math.Sqrt(result)
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ func NewRBFKernel(gamma float64) *RBFKernel {
|
||||
|
||||
// InnerProduct computes the inner product through a kernel trick
|
||||
// K(x, y) = exp(-gamma * ||x - y||^2)
|
||||
func (self *RBFKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
func (r *RBFKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||
euclidean := NewEuclidean()
|
||||
distance := euclidean.Distance(vectorX, vectorY)
|
||||
|
||||
result := math.Exp(-self.gamma * math.Pow(distance, 2))
|
||||
result := math.Exp(-r.gamma * math.Pow(distance, 2))
|
||||
|
||||
return result
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user