1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-30 13:48:57 +08:00
golearn/utilities/distance.go

37 lines
757 B
Go
Raw Normal View History

2014-05-01 07:46:13 +01:00
package utilities
import (
"fmt"
"math"
2014-05-01 19:56:30 +01:00
mat "github.com/skelterjohn/go.matrix"
2014-05-01 07:46:13 +01:00
)
2014-05-01 19:56:30 +01:00
// Computes the 'distance' between two vectors, where the distance is one of the following methods -
// euclidean (more to come)
func ComputeDistance(metric string, vector *mat.DenseMatrix, testrow *mat.DenseMatrix) (float64, error) {
2014-05-01 07:46:13 +01:00
var sum float64
2014-05-01 19:56:30 +01:00
switch metric {
case "euclidean":
{
difference, err := testrow.MinusDense(vector)
flat := difference.Array()
2014-05-01 07:46:13 +01:00
2014-05-01 19:56:30 +01:00
if err != nil {
fmt.Println(err)
}
2014-05-01 07:46:13 +01:00
2014-05-01 19:56:30 +01:00
for _, i := range flat {
squared := math.Pow(i, 2)
sum += squared
}
2014-05-01 07:46:13 +01:00
2014-05-01 19:56:30 +01:00
eucdistance := math.Sqrt(sum)
return eucdistance, nil
}
default:
return 0.0, fmt.Errorf("ValueError: %s is not an implemented distance method", metric)
}
}