By Rick Date: April 4, 2015
Machine Learning can be intimidating. One area of ML that is easy to try out as a newcomer is the Naive Bayes family of classifiers.
Instead of going into the how and why of Naive Bayes classifiers, I will demonstrate a real world example of text categorization using Ruby.
Generally speaking, Naive Bayes classifiers are seen in applications such as:
We have had great luck with the “Classifier” gem. Let’s try it out.
In the following example, we use the Classifier gem to determine if a particular quote can be attributed to Paris Hilton or Benjamin Franklin. For the sake of brevity, I have removed ambiguous training data. If you would like to see the full set of training data*, check out this gist on GitHub.
Once you have installed the gem via
gem install classifier, you can run the code below to classify quotes:
require 'classifier' robot_overlord = Classifier::Bayes.new 'hilton', 'franklin' # Train your classifier using as many quotes as possible... robot_overlord.train_hilton("The only rule is don't be boring and "\ "dress cute wherever you go. Life is "\ "too short to blend in.") robot_overlord.train_franklin("Tell me and I forget. Teach me and "\ "I remember. Involve me and I learn.") robot_overlord.classify "He that is good for making excuses is "\ "seldom good for anything else." # => :franklin robot_overlord.classify "One night stands are not for me. I think "\ "it's gross when you just give it up." # => :hilton
Naive Bayes classifiers are not a magic wand that can solve any problem, but they are definitely worth a look if you want to see machine learning in action. We’d love to hear about the things you build.
Our example is enough to move on to problems like spam control, auto text tagging, language detection and even sentiment analysis. If you are more interested in the theoretical aspects of Naive Bayes classifiers, there are some great video lectures available.