MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / Bayes

Class Bayes

supervised_class/bayes.py:18–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16from scipy.stats import multivariate_normal as mvn
17
18class Bayes(object):
19 def fit(self, X, Y, smoothing=1e-2):
20 N, D = X.shape
21 self.gaussians = dict()
22 self.priors = dict()
23 labels = set(Y)
24 for c in labels:
25 current_x = X[Y == c]
26 self.gaussians[c] = {
27 'mean': current_x.mean(axis=0),
28 'cov': np.cov(current_x.T) + np.eye(D)*smoothing,
29 }
30 self.priors[c] = float(len(Y[Y == c])) / len(Y)
31
32 def score(self, X, Y):
33 P = self.predict(X)
34 return np.mean(P == Y)
35
36 def predict(self, X):
37 N, D = X.shape
38 K = len(self.gaussians)
39 P = np.zeros((N, K))
40 for c, g in iteritems(self.gaussians):
41 mean, cov = g['mean'], g['cov']
42 P[:,c] = mvn.logpdf(X, mean=mean, cov=cov) + np.log(self.priors[c])
43 return np.argmax(P, axis=1)
44
45
46if __name__ == '__main__':

Callers 1

bayes.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected