Documentation
¶
Overview ¶
Package bayes implements a simple Naive Bayes classifier.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OptionLaplaceSmoothing ¶
func OptionLaplaceSmoothing[C, V cmp.Ordered](classifier *Classifier[C, V])
func OptionLogScore ¶
func OptionLogScore[C, V cmp.Ordered](classifier *Classifier[C, V])
func OptionProbScore ¶
func OptionProbScore[C, V cmp.Ordered](classifier *Classifier[C, V])
Types ¶
type Classifier ¶
Example ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int]()
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
for key, val := range classifier.Scores([]int{1, 2, 3}) {
fmt.Println(key, val)
}
}
Output: c -1.0986122886681098 a -77.08392035747161 b -77.08392035747161
Example (LaplaceSmoothing) ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int](bayes.OptionLaplaceSmoothing)
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
for key, val := range classifier.Scores([]int{1, 2, 3}) {
fmt.Println(key, val)
}
}
Output: c -0.5516476182862461 a -77.08392035747161 b -77.08392035747161
Example (ProbScore) ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int](bayes.OptionProbScore)
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
for key, val := range classifier.Scores([]int{1, 2, 3}) {
fmt.Println(key, val)
}
}
Output: c 1 a 9.999999999999999e-34 b 9.999999999999999e-34
func Load ¶
func Load[C, V cmp.Ordered](input *pb.Classifier) *Classifier[C, V]
func NewClassifier ¶
func NewClassifier[C, V cmp.Ordered](opts ...Option[C, V]) *Classifier[C, V]
func (*Classifier[C, V]) Learned ¶
func (p *Classifier[C, V]) Learned() float64
func (*Classifier[C, V]) Predict ¶
func (p *Classifier[C, V]) Predict(items []V) (C, bool)
Example ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int]()
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
fmt.Println(classifier.Predict([]int{1, 2, 3}))
}
Output: c true
Example (LaplaceSmoothing) ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int](bayes.OptionLaplaceSmoothing)
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
fmt.Println(classifier.Predict([]int{1, 2, 3}))
}
Output: c true
Example (LaplaceSmoothing_false) ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
classifier := bayes.NewClassifier[string, int](bayes.OptionLaplaceSmoothing)
classifier.Train(maps.All(map[string][]int{
"a": {7, 8, 9, 6},
"c": {1, 2, 3, 4},
"b": {4, 5, 6, 7},
}))
fmt.Println(classifier.Predict([]int{1, 2, 3, 4, 5}))
}
Output: c false
func (*Classifier[C, V]) Proto ¶
func (p *Classifier[C, V]) Proto() *pb.Classifier
nolint: cyclop
Example ¶
package main
import (
"fmt"
"maps"
"github.com/xuender/aime/bayes"
)
func main() {
class := bayes.NewClassifier[int, int]()
class.Train(maps.All(map[int][]int{1: {1, 2, 3}, 2: {3, 3}, 3: {4, 5}}))
newClass := bayes.Load[int, int](class.Proto())
fmt.Println(newClass.Learned())
}
Output: 3
func (*Classifier[C, V]) Scores ¶
func (p *Classifier[C, V]) Scores(items []V) iter.Seq2[C, float64]
func (*Classifier[C, V]) Train ¶
func (p *Classifier[C, V]) Train(seq iter.Seq2[C, []V])
type Option ¶
type Option[C, V cmp.Ordered] func(*Classifier[C, V])
Click to show internal directories.
Click to hide internal directories.