Browse Source

get result of near neighbor

master
arnaucode 7 years ago
parent
commit
1c7156d006
2 changed files with 36 additions and 25 deletions
  1. +26
    -23
      euclideanDistance.go
  2. +10
    -2
      main.go

+ 26
- 23
euclideanDistance.go

@ -1,28 +1,25 @@
package main package main
import ( import (
"fmt"
"math" "math"
) )
func euclideanDistance(dataset [][][]int, inputs [][]int) [][][]float64 {
func euclideanDistance(dataset [][][]int, inputLine []int) [][][]float64 {
var distances [][][]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
}
for _, datasetLine := range dataset {
datasetLineFloat := arrayIntToFloat64(datasetLine[0])
resultDatasetLineFloat := arrayIntToFloat64(datasetLine[1])
inputLineFloat := arrayIntToFloat64(inputLine)
r := calcDistances(inputLineFloat, datasetLineFloat)
distance := []float64{r}
var newDatasetLine [][]float64
newDatasetLine = append(newDatasetLine, datasetLineFloat)
newDatasetLine = append(newDatasetLine, resultDatasetLineFloat)
newDatasetLine = append(newDatasetLine, distance)
distances = append(distances, newDatasetLine)
//distances contains: [[datasetLine], [datasetLineResult], [distanceWithInput]]
} }
//fmt.Println(train)
return distances return distances
} }
@ -36,18 +33,24 @@ func calcDistances(in []float64, ds []float64) float64 {
return r return r
} }
func getShortestDistance(dist [][][]float64) {
min := dist[0][0]
max := dist[0][0]
func getShortestDistance(dist [][][]float64) [][]float64 {
min := dist[0][2]
max := dist[0][2]
var datasetNearResult [][]float64
for i := 0; i < len(dist); i++ { for i := 0; i < len(dist); i++ {
if dist[i][1][0] > max[0] {
//fmt.Println(dist[i][1])
if dist[i][2][0] > max[0] {
max = dist[i][0] max = dist[i][0]
} }
if dist[i][0][0] < min[0] {
/*fmt.Println(min[0])
fmt.Println(dist[i][1][0])*/
if dist[i][2][0] < min[0] {
min = dist[i][0] min = dist[i][0]
datasetNearResult = dist[i]
} }
//fmt.Println(dist[i][1]) //fmt.Println(dist[i][1])
} }
fmt.Println(min)
fmt.Println(max)
/*fmt.Println(min)
fmt.Println(max)*/
return datasetNearResult
} }

+ 10
- 2
main.go

@ -1,11 +1,19 @@
package main package main
import "fmt"
func main() { func main() {
dataset := readDataset("datasets/poker/poker-hand-training-true.data", "\n", ",") dataset := readDataset("datasets/poker/poker-hand-training-true.data", "\n", ",")
//fmt.Println(dataset) //fmt.Println(dataset)
inputs := readInputs("datasets/prova.data", "\n", ",") inputs := readInputs("datasets/prova.data", "\n", ",")
//fmt.Println(inputs) //fmt.Println(inputs)
distances := euclideanDistance(dataset, inputs)
getShortestDistance(distances)
var datasetDistances [][][]float64
for _, input := range inputs {
datasetDistances = euclideanDistance(dataset, input)
r := getShortestDistance(datasetDistances)
fmt.Println(input)
fmt.Println(r)
fmt.Println(r[1])
}
} }

Loading…
Cancel
Save