Example #1
0
func MultiClassRunOnDataSet(classifier algo.MultiClassClassifier, train_dataset, test_dataset *core.DataSet, pred_path string, params map[string]string) float64 {

	if train_dataset != nil {
		classifier.Train(train_dataset)
	}

	var pred_file *os.File
	if pred_path != "" {
		pred_file, _ = os.Create(pred_path)
	}
	accuracy := 0.0
	total := 0.0
	for _, sample := range test_dataset.Samples {
		prediction := classifier.PredictMultiClass(sample)
		label, _ := prediction.KeyWithMaxValue()
		if int(label) == sample.Label {
			accuracy += 1.0
		}
		total += 1.0
		if pred_file != nil {
			pred_file.WriteString(strconv.Itoa(label) + "\n")
		}
	}
	if pred_path != "" {
		defer pred_file.Close()
	}

	return accuracy / total
}