Пример #1
0
func runRandomForest() {
	oobStatsFile := OOBStatsFile
	perfFile := PerfFile
	statFile := StatFile

	trainset, testset := getSamples()

	if MaxFeature < 0 {
		MaxFeature = trainset.GetNColumn()
	}

	for nfeature := MinFeature; nfeature < MaxFeature; nfeature++ {
		// Add prefix to OOB stats file.
		oobStatsFile = fmt.Sprintf("N%d.%s", nfeature, OOBStatsFile)

		// Add prefix to performance file.
		perfFile = fmt.Sprintf("N%d.%s", nfeature, PerfFile)

		// Add prefix to stat file.
		statFile = fmt.Sprintf("N%d.%s", nfeature, StatFile)

		// Create and build random forest.
		forest := rf.Runtime{
			Runtime: classifier.Runtime{
				RunOOB:       RunOOB,
				OOBStatsFile: oobStatsFile,
				PerfFile:     perfFile,
				StatFile:     statFile,
			},
			NTree:          NTree,
			NRandomFeature: nfeature,
			PercentBoot:    NBootstrap,
		}

		e := forest.Build(trainset)
		if e != nil {
			log.Fatal(e)
		}

		if DoTest {
			predicts, _, probs := forest.ClassifySet(testset, nil)

			forest.Performance(testset, predicts, probs)
			e = forest.WritePerformance()
			if e != nil {
				log.Fatal(e)
			}
		}
	}
}
Пример #2
0
//
// finalizeStage save forest and write the forest statistic to file.
//
func (crf *Runtime) finalizeStage(forest *rf.Runtime) (e error) {
	stat := forest.StatTotal()
	stat.ID = int64(len(crf.forests))

	e = crf.WriteOOBStat(stat)
	if e != nil {
		return e
	}

	crf.AddStat(stat)
	crf.ComputeStatTotal(stat)

	if DEBUG >= 1 {
		crf.PrintStatTotal(nil)
	}

	// (7)
	crf.AddForest(forest)

	return nil
}