Skip to content

Commit f440014

Browse files
committed
Merge pull request jekyll#54 from MadBomber/update-readme-for-threshold-feature-of-bayes-classifier
documented new threshold feature
2 parents fca6c75 + ee03485 commit f440014

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.markdown

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,65 @@ cat: cats never come when you call them
116116
cat: That dang cat keeps scratching the furniture
117117
```
118118

119+
#### Knowing the Score
120+
121+
When you ask a bayesian classifier to classify text against a set of trained categories it does so by generating a score (as a Float) for each possible category. The higher the score the closer the fit your text has with that category. The category with the highest score is returned as the best matching category.
122+
123+
In *ClassifierReborn* the methods *classifications* and *classify_with_score* give you access to the calculated scores. The method *classify* only returns the best matching category.
124+
125+
Knowing the score allows you to do some interesting things. For example if your application is to generate tags for a blog post you could use the *classifications* method to get a hash of the categories and their scores. You would sort on score and take only the top 3 or 4 categories as your tags for the blog post.
126+
127+
You could within your application establish the smallest acceptable score and only use those categories whose score is greater than or equal to your smallest acceptable score as your tags for the blog post.
128+
129+
But what if you only use the *classify* method? It does not show you the score of the best category. How do you know that the best category is really any good?
130+
131+
You can use the threshold.
132+
133+
#### Using the Threshold
134+
135+
Some applications can have only one category. The application wants to know if the text being classified is of that category or not. For example consider a list of normal free text responses to some question or maybe a URL string coming to your web application. You know what a normal response looks like; but, you have no idea how people might mis-use the response. So what you want to do is create a bayesian classifier that just has one category, for example 'Good' and you want to know wither your text is classified as Good or Not Good.
136+
137+
Or suppose you just want the ability to have multiple categories and a 'None of the Above' as a possibility.
138+
139+
##### Threshold
140+
141+
When you initialize the *ClassifierReborn::Bayes* classifier there are several options which can be set that control threshold processing.
142+
143+
```ruby
144+
b = ClassifierRebor::Bayes.new(
145+
'good', # one or more categories
146+
enable_threshold: true, # default: false
147+
threshold: -10.0 # default: 0.0
148+
)
149+
b.train_good 'good stuff from Dobie Gillis'
150+
# ...
151+
text = 'bad junk from Maynard G. Krebs'
152+
result = b.classify text
153+
if result.nil?
154+
STDERR.puts "ALERT: This is not good: #{text}"
155+
let_loose_the_dogs_of_war! # method definition left to the reader
156+
end
157+
158+
```
159+
160+
In the *classify* method when the best category for the text has a score that is either less than the established threshold or is Float::INIFINITY, a nil category is returned. When you see a nil value returned from the *classify* method it means that none of the trained categories (regardless or how many categories were trained) has a score that is above or equal to the established threshold.
161+
162+
#### Other Threshold-related Convience Methods
163+
164+
```ruby
165+
b.threshold # get the current threshold
166+
b.threshold = -10.0 # set the threshold
167+
b.threshold_enabled? # Boolean: is the threshold enabled?
168+
b.threshold_disabled? # Boolean: is the threshold disabled?
169+
b.enable_threshold # enables threshold processing
170+
b.disable_threshold # disables threshold processing
171+
```
172+
173+
Using these convience methods your applications can dynamically adjust threshold processing as required.
174+
119175
### Bayesian Classification
120176

177+
* https://en.wikipedia.org/wiki/Naive_Bayes_classifier
121178
* http://www.process.com/precisemail/bayesian_filtering.htm
122179
* http://en.wikipedia.org/wiki/Bayesian_filtering
123180
* http://www.paulgraham.com/spam.html

0 commit comments

Comments
 (0)