classifier
classifier copied to clipboard
Save and load a classifier
Save and load a classifier to/from a binary file.
I had to make some of the attributes public to do this.
Example:
classifier := naive.New()
classifier.TrainString("The quick brown fox jumped over the lazy dog", "ham")
classifier.TrainString("Earn a degree online", "ham")
classifier.TrainString("Earn cash quick online", "spam")
classifier.Save("test.bin")
// ...later...
classifier, err := naive.Load("test.bin")
if classification, err := classifier.ClassifyString("Earn your masters degree online"); err == nil {
fmt.Println("Classification => ", classification) // ham
} else {
fmt.Println("error: ", err)
}
Should have tested this on real data 😉
While the provided example does work as expected, trying this on other data yields inconsistent results between pre-saved/post-loaded classifiers.
edit: I'm actually getting inconsistent results from the same test input whether or not I save. I would have thought it would give the same classification every time. I'm just an ML n00b and this is probably expected, at least with a small sample size
Hey @jamesalbert - thanks for submitting the pull request. I had been meaning to add support for persistence, but hadn't had a chance; much appreciated. If you run the same exact data through every time, you should get the same results. If you're not, we have a problem. Is there any way that you can provide details about the data set that you're using?