Skip to content

CyrusF/go-bayesian

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

It was based on RadhiFadlillah's project, and this one was added a new funtion of load model from a byte slice. It is useful to build project into a single binary file.

Usage Examples

For basic classifying, you can do it like this:

import (
	"fmt"
	"github.com/CyrusF/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)
	}
}

or from the io.Reader or byte slice

func main() {
	// New classifier from io.Reader
	byteReader := bytes.NewReader([]byte(""))
	classifier, err := bayesian.NewClassifierFromFileStream(byteReader)
	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

About

Naive-Bayes classifier for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%