-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoricalEstimator.java
More file actions
52 lines (47 loc) · 1.6 KB
/
Copy pathCategoricalEstimator.java
File metadata and controls
52 lines (47 loc) · 1.6 KB
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
public class CategoricalEstimator extends Estimator{
public Double count[][];//attribute index & classindex
public Double classcount[];//count of the class itself
public int sizeofExamples=0;
int numofClasses;
Double weight[];
DataSet dataset;
int sizeofclass;
CategoricalEstimator(DataSet dataset,int index, int classIndex, Double w[]) throws Exception
{
this.weight = w;
int sizeofAttribute = dataset.attributes.get(index).getSize();
sizeofclass = dataset.attributes.get(classIndex).getSize();
this.dataset = dataset;
sizeofExamples = dataset.examples.size()+sizeofclass;// add 1 smooth
count = new Double[sizeofAttribute][sizeofclass];//store counts
for(int i=0;i<sizeofAttribute;i++)
for(int j=0;j<sizeofclass;j++)
count[i][j]=1.0;// add 1 smooth
classcount = new Double[sizeofclass];
for(int i=0;i<sizeofclass;i++)
{
classcount[i]=1.0;//add 1 smooth for prior
}
for(int i=0;i<dataset.examples.size();i++)// go through examples
{
Example e = dataset.examples.get(i);
int attributeindex = e.get(index).intValue();
int classindex = e.get(classIndex).intValue();
addValue(attributeindex,classindex,i);
}
}
void addValue(int i, int j, int index)
{
count[i][j]+=weight[index]*dataset.examples.size();
classcount[j]+=weight[index]*dataset.examples.size();
}
Double getProbability(int k)//prior
{
return classcount[k]/sizeofExamples;//already smoothed in construction method
}
public Double getProbability(int i, int j)//condition
{
//System.out.println("i j "+i+" "+j+" "+count[i][j]/classcount[j]);
return count[i][j]/(classcount[j]+sizeofclass+1);
}
}