Example #1
0
func ExamplePGM7_LogisticRegression_HoldoutTesting() {
	rand.Seed(98765)
	trainFeatures, trainLabels := readTrainData()

	for _, testingFraction := range []float64{0.5, 0.25, 0.1, 0.05} {
		fmt.Println("---\nfraction: ", testingFraction)
		score := ai.HoldoutTestBinaryClassifier(
			trainFeatures,
			trainLabels,
			testingFraction,
			ai.NewLogisticRegressionTrainer(0,
				&sgrad.NumIterationsCrit{NumIterations: 10},
				1e-8))
		fmt.Println("Holdout testing:", score)
	}

	// Output:
	// ---
	// fraction:  0.5
	// Holdout testing: 0.84
	// ---
	// fraction:  0.25
	// Holdout testing: 0.92
	// ---
	// fraction:  0.1
	// Holdout testing: 0.85
	// ---
	// fraction:  0.05
	// Holdout testing: 1
}
Example #2
0
func ExamplePGM7_LogisticRegression_OptimizeLambda() {
	rand.Seed(98765)
	trainFeatures, trainLabels := readTrainData()
	benchmarkFeatures, benchmarkLabels := readBenchmarkData()

	goalFunc := func(lambda float64) float64 {
		score := ai.HoldoutTestBinaryClassifier(
			trainFeatures,
			trainLabels,
			.1,
			ai.NewLogisticRegressionTrainer(
				lambda,
				&sgrad.NumIterationsCrit{NumIterations: 10},
				1e-8))
		return -score
	}

	lambda := gssearh.Minimize(0, 10, goalFunc, &gssearh.AbsoluteErrorTermCrit{}, .1)
	fmt.Println("Optimal lambda:", lambda)
	classifier := ai.TrainLogisticRegressionClassifier(
		trainFeatures,
		trainLabels,
		lambda,
		&sgrad.RelativeMeanImprovementCrit{},
		1e-2)
	fmt.Println("train set: ", ai.EvaluateBinaryClassifier(classifier, trainFeatures, trainLabels))
	fmt.Println("benchmark set: ", ai.EvaluateBinaryClassifier(classifier, benchmarkFeatures, benchmarkLabels))

	// Output:
	// Optimal lambda: 1.4841053312063623
	// train set:  0.895
	// benchmark set:  0.9
}