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

Class NaiveBayes

supervised_class/nb.py:17–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

nb.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected