2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* NaiveBayesMultinomialUpdateable.java
19
* Copyright (C) 2003 University of Waikato, Hamilton, New Zealand
20
* Copyright (C) 2007 Jiang Su (incremental version)
23
package weka.classifiers.bayes;
25
import weka.classifiers.UpdateableClassifier;
26
import weka.core.Instance;
27
import weka.core.Instances;
28
import weka.core.Utils;
31
<!-- globalinfo-start -->
32
* Class for building and using a multinomial Naive Bayes classifier. For more information see,<br/>
34
* Andrew Mccallum, Kamal Nigam: A Comparison of Event Models for Naive Bayes Text Classification. In: AAAI-98 Workshop on 'Learning for Text Categorization', 1998.<br/>
36
* The core equation for this classifier:<br/>
38
* P[Ci|D] = (P[D|Ci] x P[Ci]) / P[D] (Bayes rule)<br/>
40
* where Ci is class i and D is a document.<br/>
42
* Incremental version of the algorithm.
44
<!-- globalinfo-end -->
46
<!-- technical-bibtex-start -->
49
* @inproceedings{Mccallum1998,
50
* author = {Andrew Mccallum and Kamal Nigam},
51
* booktitle = {AAAI-98 Workshop on 'Learning for Text Categorization'},
52
* title = {A Comparison of Event Models for Naive Bayes Text Classification},
57
<!-- technical-bibtex-end -->
59
<!-- options-start -->
60
* Valid options are: <p/>
63
* If set, classifier is run in debug mode and
64
* may output additional info to the console</pre>
68
* @author Andrew Golightly (acg4@cs.waikato.ac.nz)
69
* @author Bernhard Pfahringer (bernhard@cs.waikato.ac.nz)
71
* @version $Revision: 1.2 $
73
public class NaiveBayesMultinomialUpdateable
74
extends NaiveBayesMultinomial
75
implements UpdateableClassifier {
77
/** for serialization */
78
private static final long serialVersionUID = -7204398796974263186L;
80
/** the word count per class */
81
protected double[] m_wordsPerClass;
84
* Returns a string describing this classifier
86
* @return a description of the classifier suitable for
87
* displaying in the explorer/experimenter gui
89
public String globalInfo() {
91
super.globalInfo() + "\n\n"
92
+ "Incremental version of the algorithm.";
96
* Generates the classifier.
98
* @param instances set of instances serving as training data
99
* @throws Exception if the classifier has not been generated successfully
101
public void buildClassifier(Instances instances) throws Exception {
102
// can classifier handle the data?
103
getCapabilities().testWithFail(instances);
105
// remove instances with missing class
106
instances = new Instances(instances);
107
instances.deleteWithMissingClass();
109
m_headerInfo = new Instances(instances, 0);
110
m_numClasses = instances.numClasses();
111
m_numAttributes = instances.numAttributes();
112
m_probOfWordGivenClass = new double[m_numClasses][];
113
m_wordsPerClass = new double[m_numClasses];
114
m_probOfClass = new double[m_numClasses];
116
// initialising the matrix of word counts
117
// NOTE: Laplace estimator introduced in case a word that does not
118
// appear for a class in the training set does so for the test set
120
for (int c = 0; c < m_numClasses; c++) {
121
m_probOfWordGivenClass[c] = new double[m_numAttributes];
122
m_probOfClass[c] = laplace;
123
m_wordsPerClass[c] = laplace * m_numAttributes;
124
for(int att = 0; att<m_numAttributes; att++) {
125
m_probOfWordGivenClass[c][att] = laplace;
129
for (int i = 0; i < instances.numInstances(); i++)
130
updateClassifier(instances.instance(i));
134
* Updates the classifier with the given instance.
136
* @param instance the new training instance to include in the model
137
* @throws Exception if the instance could not be incorporated in
140
public void updateClassifier(Instance instance) throws Exception {
141
int classIndex = (int) instance.value(instance.classIndex());
142
m_probOfClass[classIndex] += instance.weight();
144
for (int a = 0; a < instance.numValues(); a++) {
145
if (instance.index(a) == instance.classIndex() ||
146
instance.isMissing(a))
149
double numOccurences = instance.valueSparse(a) * instance.weight();
150
if (numOccurences < 0)
152
"Numeric attribute values must all be greater or equal to zero.");
153
m_wordsPerClass[classIndex] += numOccurences;
154
m_probOfWordGivenClass[classIndex][instance.index(a)] += numOccurences;
159
* Calculates the class membership probabilities for the given test
162
* @param instance the instance to be classified
163
* @return predicted class probability distribution
164
* @throws Exception if there is a problem generating the prediction
166
public double[] distributionForInstance(Instance instance) throws Exception {
167
double[] probOfClassGivenDoc = new double[m_numClasses];
169
// calculate the array of log(Pr[D|C])
170
double[] logDocGivenClass = new double[m_numClasses];
171
for (int c = 0; c < m_numClasses; c++) {
172
logDocGivenClass[c] += Math.log(m_probOfClass[c]);
174
for (int i = 0; i < instance.numValues(); i++) {
175
if (instance.index(i) == instance.classIndex())
177
double frequencies = instance.valueSparse(i);
178
allWords += frequencies;
179
logDocGivenClass[c] += frequencies *
180
Math.log(m_probOfWordGivenClass[c][instance.index(i)]);
182
logDocGivenClass[c] -= allWords * Math.log(m_wordsPerClass[c]);
185
double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)];
186
for (int i = 0; i < m_numClasses; i++)
187
probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max);
189
Utils.normalize(probOfClassGivenDoc);
191
return probOfClassGivenDoc;
195
* Returns a string representation of the classifier.
197
* @return a string representation of the classifier
199
public String toString() {
200
StringBuffer result = new StringBuffer();
202
result.append("The independent probability of a class\n");
203
result.append("--------------------------------------\n");
205
for (int c = 0; c < m_numClasses; c++)
206
result.append(m_headerInfo.classAttribute().value(c)).append("\t").
207
append(Double.toString(m_probOfClass[c])).append("\n");
209
result.append("\nThe probability of a word given the class\n");
210
result.append("-----------------------------------------\n\t");
212
for (int c = 0; c < m_numClasses; c++)
213
result.append(m_headerInfo.classAttribute().value(c)).append("\t");
217
for (int w = 0; w < m_numAttributes; w++) {
218
result.append(m_headerInfo.attribute(w).name()).append("\t");
219
for (int c = 0; c < m_numClasses; c++)
221
Double.toString(Math.exp(m_probOfWordGivenClass[c][w]))).append("\t");
225
return result.toString();
229
* Main method for testing this class.
231
* @param args the options
233
public static void main(String[] args) {
234
runClassifier(new NaiveBayesMultinomialUpdateable(), args);