My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package iweb2.ch5.classification.bayes;

import iweb2.ch5.classification.core.TrainingSet;
import iweb2.ch5.classification.core.intf.Classifier;
import iweb2.ch5.ontology.core.AttributeValue;
import iweb2.ch5.ontology.intf.Attribute;
import iweb2.ch5.ontology.intf.Concept;
import iweb2.ch5.ontology.intf.Instance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* A basic implementation of the Naive Bayes algorithm.
*
* The emphasis is on teaching the algorithm, not optimizing its performance.
*
* @author babis
*/
public class NaiveBayes implements Classifier {

/**
* You can use the NaiveBayes classifier in many occasions
* So, let's give it a name to identify the instance of the Classifier.
*/
private String name;

/**
* Every classifier needs a training set. Notice that both the name
* of the classifier and its training set are intentionally set during
* the Construction phase.
*
* Once you created an instance of the NaiveBayes classifier you cannot
* set its TrainingSet but you can always get the reference to it and
* add instances.
*/
protected TrainingSet tSet;

/**
* These are the probabilities for each concept
*/
protected Map<Concept,Double> conceptPriors;

/**
* This structure contains the fundamental calculation elements of
* the Naive Bayes method, i.e. the conditional probabilities.
*/
protected Map<Concept,Map<Attribute, AttributeValue>> p;

/**
* These are the attribute indices that we should consider for training
*/
protected ArrayList<String> attributeList;

/** An auxiliary variable */
protected boolean verbose = false;

/**
* The only constructor for this classifier takes a name and
* a training set as arguments.
*
* @param name the name of the classifier
* @param set the training set for this classifier
*/
public NaiveBayes(String name, TrainingSet set) {

this.name = name;
tSet = set;

conceptPriors = new HashMap<Concept,Double>(tSet.getNumberOfConcepts());
verbose = false;
}

public Concept classify(Instance instance) {

Concept bestConcept = null;
double bestP = 0.0;

if( tSet == null || tSet.getConceptSet().size() == 0) {
throw new RuntimeException("You have to train classifier first.");
}
if( verbose ) {
System.out.println("\n*** Classifying instance: " + instance.toString() + "\n");
}
for (Concept c : tSet.getConceptSet()) {
double p = getProbability(c, instance);
if( verbose ) {
System.out.printf("P(%s|%s) = %.15f\n", c.getName(), instance.toString(), p);
}
if( p >= bestP ) {
bestConcept = c;
bestP = p;
}
}
return bestConcept;
}

/**
* Training simply sets the probability for each concept
*
*/
public boolean train() {

long t0 = System.currentTimeMillis();

boolean hasTrained = false;

if ( attributeList == null || attributeList.size() == 0) {

System.out.print("Can't train the classifier without specifying the attributes for training!");
System.out.print("Use the method --> trainOnAttribute(Attribute a)");

} else {

calculateConceptPriors();

calculateConditionalProbabilities();

hasTrained = true;
}

if (verbose) {
System.out.print(" Naive Bayes training completed in ");
System.out.println((System.currentTimeMillis()-t0)+" (ms)");
}

return hasTrained;
}

public void trainOnAttribute(String aName) {

if (attributeList ==null) {
attributeList = new ArrayList<String>();
}

attributeList.add(aName);
}

/**
* Strictly speaking these are not the prior probabilities but just the counts.
* However, we want to reuse these counts and the priors can be obtained by a simple division.
*/
private void calculateConceptPriors() {

for (Concept c : tSet.getConceptSet()) {

//Calculate the priors for the concepts
int totalConceptCount=0;

for (Instance i : tSet.getInstances().values()) {

if (i.getConcept().equals(c)) {
totalConceptCount++;
}
}

conceptPriors.put(c, new Double(totalConceptCount));
}
}

protected void calculateConditionalProbabilities() {

p = new HashMap<Concept, Map<Attribute, AttributeValue>>();

for (Instance i : tSet.getInstances().values()) {

for (Attribute a: i.getAtrributes()) {

if (a != null && attributeList.contains(a.getName())) {

if ( p.get(i.getConcept())== null ) {

p.put(i.getConcept(), new HashMap<Attribute, AttributeValue>());

}

Map<Attribute, AttributeValue> aMap = p.get(i.getConcept());
AttributeValue aV = aMap.get(a);
if ( aV == null ) {

aV = new AttributeValue(a.getValue());
aMap.put(a, aV);

} else {
aV.count();
}
}
}
}
}

/**
* This method calculates the <I>posterior probability</I> that we deal with
* concept <CODE>c</CODE> provided that we observed instance <CODE>i</CODE>.
* This is the application of Bayes theorem.
*
* @param c is a probable concept for instance <CODE>i</CODE>
* @param i is the observed instance
* @return posterior probability of <CODE>c</CODE> given instance <CODE>i</CODE>
*/
public double getProbability(Concept c, Instance i) {

double cP=0;

if (tSet.getConceptSet().contains(c)) {

cP = (getProbability(i,c)*getProbability(c))/getProbability(i);

} else {
// We have never seen this concept before
// assign to it a "reasonable" value
cP = 1/(tSet.getNumberOfConcepts()+1.0);
}

return cP;
}

/**
* This method calculates the denumerator of Bayes theorem
*
* @param <CODE>Instance</CODE> i
* @return the probability of observing <CODE>Instance</CODE> i
*/
public double getProbability(Instance i) {

double cP=0;

for (Concept c : getTset().getConceptSet()) {

cP += getProbability(i,c)*getProbability(c);
}
return (cP == 0) ? (double)1/tSet.getSize() : cP;
}

public double getProbability(Concept c) {
Double trInstanceCount = conceptPriors.get(c);
if( trInstanceCount == null ) {
trInstanceCount = 0.0;
}
return trInstanceCount/tSet.getSize();
}

public double getProbability(Instance i, Concept c) {

double cP=1;

for (Attribute a : i.getAtrributes()) {

if ( a != null && attributeList.contains(a.getName()) ) {

Map<Attribute, AttributeValue> aMap = p.get(c);
AttributeValue aV = aMap.get(a);
if ( aV == null) {
// the specific attribute value is not present for the current concept.
// Can you justify the following estimate?
// Can you think of a better choice?
cP *= ((double) 1 / (tSet.getSize()+1));
} else {
cP *= (aV.getCount()/conceptPriors.get(c));
}
}
}

return (cP == 1) ? (double)1/tSet.getNumberOfConcepts() : cP;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @return the tSet
*/
public TrainingSet getTset() {
return tSet;
}
}
Show details Hide details

Change log

r8 by dbabenko on Jan 17, 2009   Diff
This code release is for final chapter
versions of the book "Algorithms of the
Intelligent Web".
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by aristotle.of.stageira on Sep 14, 2008   Diff
Creation of the project. The code in
this distribution covers chapters 1
through 6 of the book "Algorithms of
the Intelligent Web"
All revisions of this file

File info

Size: 7112 bytes, 281 lines

File properties

svn:eol-style
native
Hosted by Google Code