Method
fit
(self, X, Y, smoothing=1e-2)
Source from the content-addressed store, hash-verified
| 17 | |
| 18 | class 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) |
Tested by
no test coverage detected