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.

53 lines
1.2 KiB

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