Giter Site home page Giter Site logo

go-bayesian's Introduction

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

go-bayesian's People

Contributors

radhifadlillah avatar cyrusf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.