mirror of
https://github.com/arnaucube/goKNN.git
synced 2026-02-06 19:16:44 +01:00
reading file and converting to array of dataset
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
datasets
|
||||
9
errorsCheck.go
Normal file
9
errorsCheck.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
5
euclideanDistance.go
Normal file
5
euclideanDistance.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func euclideanDistance(dataset [][][]int) {
|
||||
|
||||
}
|
||||
8
main.go
Normal file
8
main.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
dataset := readDataFile("datasets/poker/poker-hand-training-true.data", "\n", ",")
|
||||
fmt.Println(dataset)
|
||||
}
|
||||
35
readDataFile.go
Normal file
35
readDataFile.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func readDataFile(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
|
||||
var lastLineValue []int
|
||||
for k2, v2 := range params {
|
||||
value, err := strconv.Atoi(v2)
|
||||
check(err)
|
||||
if k2 < len(params)-1 {
|
||||
datasetLineEntry = append(datasetLineEntry, value)
|
||||
} else {
|
||||
lastLineValue = append(lastLineValue, value)
|
||||
}
|
||||
}
|
||||
datasetLine = append(datasetLine, datasetLineEntry)
|
||||
datasetLine = append(datasetLine, lastLineValue)
|
||||
dataset = append(dataset, datasetLine)
|
||||
}
|
||||
return dataset
|
||||
}
|
||||
Reference in New Issue
Block a user