Navigation Menu

Skip to content

Commit

Permalink
Remove the package variable minClassSize (#17)
Browse files Browse the repository at this point in the history
* Add more documents to example in README

* Make minimum class size a property on the classifier

* Remove small typos in example comments
  • Loading branch information
Allison Morgan authored and drewlanenga committed Aug 19, 2016
1 parent 540b4e6 commit 539efb0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -48,6 +48,7 @@ documents := []struct {
}

classifier := NewClassifier()
classifier.MinClassSize = 0

// train the classifier
for _, document := range documents {
Expand Down
6 changes: 4 additions & 2 deletions bayes.go
Expand Up @@ -6,12 +6,13 @@ import (

var (
smoother = 1 // laplace
minClassSize = 5
defaultMinClassSize = 5
)

type Classifier struct {
Tokenizer *tokenizer `json:"-"`
Matrix *sparseMatrix `json:"matrix"`
MinClassSize int
}

// Create a new multibayes classifier.
Expand All @@ -25,6 +26,7 @@ func NewClassifier() *Classifier {
return &Classifier{
Tokenizer: tokenize,
Matrix: sparse,
MinClassSize: defaultMinClassSize,
}
}

Expand All @@ -41,7 +43,7 @@ func (c *Classifier) Posterior(document string) map[string]float64 {
predictions := make(map[string]float64)

for class, classcolumn := range c.Matrix.Classes {
if len(classcolumn.Data) < minClassSize {
if len(classcolumn.Data) < c.MinClassSize {
continue
}

Expand Down
3 changes: 1 addition & 2 deletions bayes_test.go
Expand Up @@ -6,9 +6,8 @@ import (
)

func TestPosterior(t *testing.T) {
minClassSize = 0

classifier := NewClassifier()
classifier.MinClassSize = 0
classifier.trainWithTestData()

probs := classifier.Posterior("Aaron's dog has tons of fleas")
Expand Down
1 change: 1 addition & 0 deletions example_test.go
Expand Up @@ -24,6 +24,7 @@ func Example() {
}

classifier := NewClassifier()
classifier.MinClassSize = 0

// train the classifier
for _, document := range documents {
Expand Down

0 comments on commit 539efb0

Please sign in to comment.