Esempio n. 1
0
func readShuffled(in io.Reader, rand *rand.Rand) []TrainingInstance {
	reader, err := input.NewTrainDataReader(in)
	common.ExitIfError("Error reading data: ", err)

	instances := make([]TrainingInstance, 0)

	for {
		err := reader.Scan()
		if err == io.EOF {
			break
		} else {
			common.ExitIfError("Error reading data: ", err)
		}

		instance := TrainingInstance{
			X: reader.InputVector().Dup(),
			Y: reader.Label(),
		}

		idx := rand.Int63n(int64(len(instances)) + 1)
		if int(idx) == len(instances) {
			instances = append(instances, instance)
		} else {
			instances = append(instances, instances[idx])
			instances[idx] = instance
		}
	}

	return instances
}
Esempio n. 2
0
func processData(filename string, fun dataFun) error {
	f, err := os.Open(filename)
	if err != nil {
		return err
	}

	bar, err := common.CreateFileProgress(f)
	defer bar.Finish()
	bar.Start()

	reader, err := input.NewTrainDataReader(bar.NewProxyReader(bufio.NewReader(f)))
	if err != nil {
		return err
	}

	for {
		if err := reader.Scan(); err == io.EOF {
			break
		} else if err != nil {
			return err
		}

		if err := fun(reader.Label(), reader.InputVector()); err != nil {
			return err
		}
	}

	return nil
}
Esempio n. 3
0
func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	common.ExitIfError("Could not open data file: ", err)
	defer f.Close()

	reader, err := input.NewTrainDataReader(f)
	common.ExitIfError("Error reading data: ", err)

	for {
		err := reader.Scan()
		if err == io.EOF {
			break
		} else {
			common.ExitIfError("Error reading data: ", err)
		}

		if *features {
			fmt.Printf("%d %s\n", reader.Label(), floatSliceToString(reader.InputVector().Layer(addr.FEATURE)))
		} else {
			fmt.Printf("%d %s\n", reader.Label(), floatSliceToString(reader.InputVector().All()))
		}
	}

}