mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +08:00
We will need to implement many metrics, so we should split it into a package.
This commit is contained in:
parent
8f1de1ba2c
commit
0eaf944764
26
examples/metrics/metrics.go
Normal file
26
examples/metrics/metrics.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
pariwiseMetrics "github.com/sjwhitworth/golearn/metrics/pairwise"
|
||||||
|
"github.com/sjwhitworth/golearn/utilities"
|
||||||
|
mat "github.com/skelterjohn/go.matrix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randArray := utilities.RandomArray(3, 7)
|
||||||
|
vectorX := mat.MakeDenseMatrix(randArray, 1, 3)
|
||||||
|
randArray = utilities.RandomArray(3, 7)
|
||||||
|
vectorY := mat.MakeDenseMatrix(randArray, 1, 3)
|
||||||
|
|
||||||
|
distance, err := pariwiseMetrics.Euclidean(vectorX, vectorY)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Euclidean distance of " + vectorX.String() + " and " + vectorY.String() + " is: ")
|
||||||
|
fmt.Println(distance)
|
||||||
|
}
|
30
metrics/pairwise/euclidean.go
Normal file
30
metrics/pairwise/euclidean.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package pairwise
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
mat "github.com/skelterjohn/go.matrix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// We may need to create Metrics / Vector interface for this
|
||||||
|
func Euclidean(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
|
||||||
|
var sum float64
|
||||||
|
|
||||||
|
difference, err := vectorY.MinusDense(vectorX)
|
||||||
|
|
||||||
|
flat := difference.Array()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, i := range flat {
|
||||||
|
squared := math.Pow(i, 2)
|
||||||
|
sum += squared
|
||||||
|
}
|
||||||
|
|
||||||
|
distance := math.Sqrt(sum)
|
||||||
|
return distance, nil
|
||||||
|
}
|
@ -1,36 +0,0 @@
|
|||||||
package utilities
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
|
|
||||||
mat "github.com/skelterjohn/go.matrix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
var sum float64
|
|
||||||
|
|
||||||
switch metric {
|
|
||||||
case "euclidean":
|
|
||||||
{
|
|
||||||
difference, err := testrow.MinusDense(vector)
|
|
||||||
flat := difference.Array()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, i := range flat {
|
|
||||||
squared := math.Pow(i, 2)
|
|
||||||
sum += squared
|
|
||||||
}
|
|
||||||
|
|
||||||
eucdistance := math.Sqrt(sum)
|
|
||||||
return eucdistance, nil
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return 0.0, fmt.Errorf("ValueError: %s is not an implemented distance method", metric)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user