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

35 lines
775 B
Go
Raw Normal View History

2017-04-07 21:11:20 +08:00
package pairwise
import (
"math"
"github.com/gonum/matrix/mat64"
)
type Cosine struct{}
func NewCosine() *Cosine {
return &Cosine{}
}
// Dot computes dot value of vectorX and vectorY.
func (c *Cosine) Dot(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
subVector := mat64.NewDense(0, 0, nil)
subVector.MulElem(vectorX, vectorY)
result := mat64.Sum(subVector)
return result
}
// Distance computes Cosine distance.
// It will return distance which represented as 1-cos() (ranged from 0 to 2).
func (c *Cosine) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
2017-04-07 21:28:56 +08:00
dotXY := c.Dot(vectorX, vectorY)
lengthX := math.Sqrt(c.Dot(vectorX, vectorX))
lengthY := math.Sqrt(c.Dot(vectorY, vectorY))
2017-04-07 21:11:20 +08:00
2017-04-07 21:28:56 +08:00
cos := dotXY / (lengthX * lengthY)
2017-04-07 21:11:20 +08:00
2017-04-07 21:28:56 +08:00
return 1 - cos
2017-04-07 21:11:20 +08:00
}