textdatareader.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package teach
  26. import (
  27. "bufio"
  28. "fmt"
  29. "log"
  30. "os"
  31. "strconv"
  32. "strings"
  33. mat "gonum.org/v1/gonum/mat"
  34. )
  35. type TextDataReader struct {
  36. dataSet []*mat.Dense
  37. result []*mat.Dense
  38. index int
  39. }
  40. func NewTextDataReader(filename string) *TextDataReader {
  41. r := &TextDataReader{
  42. index: 0,
  43. }
  44. r.readData(filename)
  45. return r
  46. }
  47. func (r *TextDataReader) readData(filename string) {
  48. inputFile, err := os.Open(filename)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. defer inputFile.Close()
  53. scanner := bufio.NewScanner(inputFile)
  54. scanner.Split(bufio.ScanLines)
  55. max := []float64{0.0, 0.0, 0.0, 0.0}
  56. for scanner.Scan() {
  57. dataLine := scanner.Text()
  58. data := strings.Split(dataLine, ",")
  59. if len(data) < 5 {
  60. fmt.Printf("Garbage record: %s\n", dataLine)
  61. continue
  62. }
  63. var dataRaw []float64
  64. for i := 0; i < 4; i++ {
  65. val, err := strconv.ParseFloat(data[i], 64)
  66. if err != nil {
  67. break
  68. }
  69. dataRaw = append(dataRaw, val)
  70. if max[i] < val {
  71. max[i] = val
  72. }
  73. }
  74. if len(dataRaw) < 4 {
  75. fmt.Printf("Garbage record: %s\n", dataLine)
  76. continue
  77. }
  78. r.dataSet = append(r.dataSet, mat.NewDense(4, 1, dataRaw))
  79. switch data[4] {
  80. case "Iris-setosa":
  81. r.result = append(r.result, mat.NewDense(3, 1, []float64{1.0, 0.0, 0.0}))
  82. case "Iris-versicolor":
  83. r.result = append(r.result, mat.NewDense(3, 1, []float64{0.0, 1.0, 0.0}))
  84. case "Iris-virginica":
  85. r.result = append(r.result, mat.NewDense(3, 1, []float64{0.0, 0.0, 1.0}))
  86. }
  87. }
  88. //normalize
  89. for i := 0; i < len(r.dataSet); i++ {
  90. r.dataSet[i].Apply(func(r, _ int, val float64) float64 {
  91. return val / max[r]
  92. }, r.dataSet[i])
  93. }
  94. }
  95. func (r *TextDataReader) GetData() *mat.Dense {
  96. return r.dataSet[r.index]
  97. }
  98. func (r *TextDataReader) GetExpect() *mat.Dense {
  99. return r.result[r.index]
  100. }
  101. func (r *TextDataReader) Next() bool {
  102. r.index++
  103. if r.index >= len(r.result) {
  104. r.index = 0
  105. return false
  106. }
  107. return true
  108. }
  109. func (r *TextDataReader) Reset() {
  110. r.index = 0
  111. }
  112. func (r *TextDataReader) Index() int {
  113. return r.index
  114. }