|
3 | 3 |
|
4 | 4 | class BayesClassifier(object):
|
5 | 5 |
|
6 |
| - def __init__(self): |
7 |
| - """ Initialize classifier with training data. """ |
8 |
| - |
9 |
| - self.labels = [] # class labels |
10 |
| - self.mean = [] # class mean |
11 |
| - self.var = [] # class variances |
12 |
| - self.n = 0 # nbr of classes |
13 |
| - |
14 |
| - def train(self,data,labels=None): |
15 |
| - """ Train on data (list of arrays n*dim). |
16 |
| - Labels are optional, default is 0...n-1. """ |
17 |
| - |
18 |
| - if labels==None: |
19 |
| - labels = range(len(data)) |
20 |
| - self.labels = labels |
21 |
| - self.n = len(labels) |
22 |
| - |
23 |
| - for c in data: |
24 |
| - self.mean.append(mean(c,axis=0)) |
25 |
| - self.var.append(var(c,axis=0)) |
26 |
| - |
27 |
| - def classify(self,points): |
28 |
| - """ Classify the points by computing probabilities |
29 |
| - for each class and return most probable label. """ |
| 6 | + def __init__(self): |
| 7 | + """ Initialize classifier with training data. """ |
30 | 8 |
|
31 |
| - # compute probabilities for each class |
32 |
| - est_prob = array([gauss(m,v,points) for m,v in zip(self.mean,self.var)]) |
33 |
| - |
34 |
| - print 'est prob',est_prob.shape,self.labels |
35 |
| - # get index of highest probability, this gives class label |
36 |
| - ndx = est_prob.argmax(axis=0) |
37 |
| - |
38 |
| - est_labels = array([self.labels[n] for n in ndx]) |
39 |
| - |
40 |
| - return est_labels, est_prob |
| 9 | + self.labels = [] # class labels |
| 10 | + self.mean = [] # class mean |
| 11 | + self.var = [] # class variances |
| 12 | + self.n = 0 # nbr of classes |
| 13 | + |
| 14 | + def train(self,data,labels=None): |
| 15 | + """ Train on data (list of arrays n*dim). |
| 16 | + Labels are optional, default is 0...n-1. """ |
| 17 | + |
| 18 | + if labels==None: |
| 19 | + labels = range(len(data)) |
| 20 | + self.labels = labels |
| 21 | + self.n = len(labels) |
| 22 | + |
| 23 | + for c in data: |
| 24 | + self.mean.append(mean(c,axis=0)) |
| 25 | + self.var.append(var(c,axis=0)) |
| 26 | + |
| 27 | + def classify(self,points): |
| 28 | + """ Classify the points by computing probabilities |
| 29 | + for each class and return most probable label. """ |
| 30 | + |
| 31 | + # compute probabilities for each class |
| 32 | + est_prob = array([gauss(m,v,points) for m,v in zip(self.mean,self.var)]) |
| 33 | + |
| 34 | + print 'est prob',est_prob.shape,self.labels |
| 35 | + # get index of highest probability, this gives class label |
| 36 | + ndx = est_prob.argmax(axis=0) |
| 37 | + |
| 38 | + est_labels = array([self.labels[n] for n in ndx]) |
| 39 | + |
| 40 | + return est_labels, est_prob |
41 | 41 |
|
42 | 42 |
|
43 | 43 | def gauss(m,v,x):
|
44 |
| - """ Evaluate Gaussian in d-dimensions with independent |
45 |
| - mean m and variance v at the points in (the rows of) x. |
46 |
| - http://en.wikipedia.org/wiki/Multivariate_normal_distribution """ |
47 |
| - |
48 |
| - if len(x.shape)==1: |
49 |
| - n,d = 1,x.shape[0] |
50 |
| - else: |
51 |
| - n,d = x.shape |
52 |
| - |
53 |
| - # covariance matrix, subtract mean |
54 |
| - S = diag(1/v) |
55 |
| - x = x-m |
56 |
| - # product of probabilities |
57 |
| - y = exp(-0.5*diag(dot(x,dot(S,x.T)))) |
58 |
| - |
59 |
| - # normalize and return |
60 |
| - return y * (2*pi)**(-d/2.0) / ( sqrt(prod(v)) + 1e-6) |
| 44 | + """ Evaluate Gaussian in d-dimensions with independent |
| 45 | + mean m and variance v at the points in (the rows of) x. |
| 46 | + http://en.wikipedia.org/wiki/Multivariate_normal_distribution """ |
| 47 | + |
| 48 | + if len(x.shape)==1: |
| 49 | + n,d = 1,x.shape[0] |
| 50 | + else: |
| 51 | + n,d = x.shape |
| 52 | + |
| 53 | + # covariance matrix, subtract mean |
| 54 | + S = diag(1/v) |
| 55 | + x = x-m |
| 56 | + # product of probabilities |
| 57 | + y = exp(-0.5*diag(dot(x,dot(S,x.T)))) |
| 58 | + |
| 59 | + # normalize and return |
| 60 | + return y * (2*pi)**(-d/2.0) / ( sqrt(prod(v)) + 1e-6) |
61 | 61 |
|
62 | 62 |
|
63 | 63 |
|
0 commit comments