You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

  1. package main
  2. import (
  3. "math"
  4. )
  5. func euclideanDistance(dataset [][][]int, inputLine []int) [][][]float64 {
  6. var distances [][][]float64
  7. for _, datasetLine := range dataset {
  8. datasetLineFloat := arrayIntToFloat64(datasetLine[0])
  9. resultDatasetLineFloat := arrayIntToFloat64(datasetLine[1])
  10. inputLineFloat := arrayIntToFloat64(inputLine)
  11. r := calcDistances(inputLineFloat, datasetLineFloat)
  12. distance := []float64{r}
  13. var newDatasetLine [][]float64
  14. newDatasetLine = append(newDatasetLine, datasetLineFloat)
  15. newDatasetLine = append(newDatasetLine, resultDatasetLineFloat)
  16. newDatasetLine = append(newDatasetLine, distance)
  17. distances = append(distances, newDatasetLine)
  18. //distances contains: [[datasetLine], [datasetLineResult], [distanceWithInput]]
  19. }
  20. return distances
  21. }
  22. func calcDistances(in []float64, ds []float64) float64 {
  23. var r float64
  24. r = 0
  25. for i := 0; i < len(in); i++ {
  26. r = r + (in[i] * ds[i])
  27. }
  28. r = math.Sqrt(r)
  29. return r
  30. }
  31. func getShortestDistance(dist [][][]float64) [][]float64 {
  32. min := dist[0][2]
  33. max := dist[0][2]
  34. var datasetNearResult [][]float64
  35. for i := 0; i < len(dist); i++ {
  36. //fmt.Println(dist[i][1])
  37. if dist[i][2][0] > max[0] {
  38. max = dist[i][0]
  39. }
  40. /*fmt.Println(min[0])
  41. fmt.Println(dist[i][1][0])*/
  42. if dist[i][2][0] < min[0] {
  43. min = dist[i][0]
  44. datasetNearResult = dist[i]
  45. }
  46. //fmt.Println(dist[i][1])
  47. }
  48. /*fmt.Println(min)
  49. fmt.Println(max)*/
  50. return datasetNearResult
  51. }