Browse Source

calculating euclidean distances

master
arnaucode 7 years ago
parent
commit
81dbde7f2b
4 changed files with 90 additions and 6 deletions
  1. +49
    -1
      euclideanDistance.go
  2. +7
    -4
      main.go
  3. +24
    -1
      readDataFile.go
  4. +10
    -0
      typeConverters.go

+ 49
- 1
euclideanDistance.go

@ -1,5 +1,53 @@
package main
func euclideanDistance(dataset [][][]int) {
import (
"fmt"
"math"
)
func euclideanDistance(dataset [][][]int, inputs [][]int) [][][]float64 {
var distances [][][]float64
for _, inputLine := range inputs {
for _, datasetLine := range dataset {
datasetLineFloat := arrayIntToFloat64(datasetLine[0])
inputLineFloat := arrayIntToFloat64(inputLine)
r := calcDistances(inputLineFloat, datasetLineFloat)
//fmt.Println(r)
rArr := []float64{r}
var newDatasetLine [][]float64
newDatasetLine = append(newDatasetLine, datasetLineFloat)
newDatasetLine = append(newDatasetLine, rArr)
distances = append(distances, newDatasetLine)
//passar tot els int de values a float64
}
}
//fmt.Println(train)
return distances
}
func calcDistances(in []float64, ds []float64) float64 {
var r float64
r = 0
for i := 0; i < len(in); i++ {
r = r + (in[i] * ds[i])
}
r = math.Sqrt(r)
return r
}
func getShortestDistance(dist [][][]float64) {
min := dist[0][0]
max := dist[0][0]
for i := 0; i < len(dist); i++ {
if dist[i][1][0] > max[0] {
max = dist[i][0]
}
if dist[i][0][0] < min[0] {
min = dist[i][0]
}
//fmt.Println(dist[i][1])
}
fmt.Println(min)
fmt.Println(max)
}

+ 7
- 4
main.go

@ -1,8 +1,11 @@
package main
import "fmt"
func main() {
dataset := readDataFile("datasets/poker/poker-hand-training-true.data", "\n", ",")
fmt.Println(dataset)
dataset := readDataset("datasets/poker/poker-hand-training-true.data", "\n", ",")
//fmt.Println(dataset)
inputs := readInputs("datasets/prova.data", "\n", ",")
//fmt.Println(inputs)
distances := euclideanDistance(dataset, inputs)
getShortestDistance(distances)
}

+ 24
- 1
readDataFile.go

@ -6,7 +6,7 @@ import (
"strings"
)
func readDataFile(path string, lineSeparation string, valueSeparation string) [][][]int {
func readDataset(path string, lineSeparation string, valueSeparation string) [][][]int {
var dataset [][][]int
b, err := ioutil.ReadFile(path)
check(err)
@ -33,3 +33,26 @@ func readDataFile(path string, lineSeparation string, valueSeparation string) []
}
return dataset
}
func readInputs(path string, lineSeparation string, valueSeparation string) [][]int {
var dataset [][]int
b, err := ioutil.ReadFile(path)
check(err)
str := string(b)
str = strings.Replace(str, "\r", "", -1)
lines := strings.Split(str, lineSeparation)
for _, v1 := range lines {
params := strings.Split(v1, valueSeparation)
//var datasetLine [][]int
var datasetLineEntry []int
for _, v2 := range params {
value, err := strconv.Atoi(v2)
check(err)
datasetLineEntry = append(datasetLineEntry, value)
}
//datasetLine = append(datasetLine, datasetLineEntry)
dataset = append(dataset, datasetLineEntry)
}
return dataset
}

+ 10
- 0
typeConverters.go

@ -0,0 +1,10 @@
package main
func arrayIntToFloat64(arr []int) []float64 {
var r []float64
for i := 0; i < len(arr); i++ {
r = append(r, float64(arr[i]))
}
return r
}

Loading…
Cancel
Save