/go-bayesian

Naive-Bayes classifier for Go

Primary LanguageGoMIT LicenseMIT

Go-Bayesian

GoDoc Build Status

Go-Bayesian is a Go package for doing classification using Naive-Bayes algorithm. There are two Naive-Bayes models that implemented in this package, which are Multinomial TF and Multinomial Boolean.

Usage Examples

For basic classifying, you can do it like this:

import (
	"fmt"
	"github.com/RadhiFadlillah/go-bayesian"
)

// Declare class
const (
	Good bayesian.Class = "good"
	Bad  bayesian.Class = "bad"
)

func main() {
	// New Multinomial TF classifier
	classifier := bayesian.NewClassifier(bayesian.MultinomialTf)

	// Do learning using two documents
	classifier.Learn(
		NewDocument(Good, "tall", "handsome", "rich"),
		NewDocument(Bad, "bald", "poor", "ugly"),
	)

	// Classify tokens from a document
	allScores, class, certain := classifier.Classify("the", "tall", "man")
	fmt.Println(allScores, class, certain)
}

You also can save the classifier to a file for later use. Useful to avoid repeating learning process :

func main() {
	// New Multinomial TF classifier
	classifier := bayesian.NewClassifier(bayesian.MultinomialTf)
	classifier.Learn(
		NewDocument(Good, "tall", "handsome", "rich"),
		NewDocument(Bad, "bald", "poor", "ugly"),
	)

	// Save classifier to file
	err := classifier.SaveClassifierToFile("./my-classifier")
	if err != nil {
		panic(err)
	}
}

Later, you can create a new Classifier from that file :

func main() {
	// New classifier from file
	classifier, err := bayesian.NewClassifierFromFile("./my-classifier")
	if err != nil {
		panic(err)
	}
}

Resource

  1. Raschka, S. 2014. Naive Bayes and Text Classification I - Introduction and Theory. (PDF and Website)
  2. Metsis, V., Androutsopoulos, I., and Paliouras, G. 2006. Spam Filtering with Naive Bayes – Which Naive Bayes ?. Proceeding of CEAS 2006 - Third Conference on Email and Anti-Spam. California, USA, July 27-28, 2006. (PDF)
  3. Lecture slides from the Stanford Coursera course by Dan Jurafsky and Christopher Manning.

License

Go-Bayesian is distributed using MIT license.

Other Naive-Bayes Implementation