| 13 | from datetime import datetime |
| 14 | |
| 15 | class MultinomialNB(object): |
| 16 | def fit(self, X, Y, smoothing=1.0): |
| 17 | # one-hot encode Y |
| 18 | K = len(set(Y)) # number of classes |
| 19 | N = len(Y) # number of samples |
| 20 | labels = Y |
| 21 | Y = np.zeros((N, K)) |
| 22 | Y[np.arange(N), labels] = 1 |
| 23 | |
| 24 | # D x K matrix of feature counts |
| 25 | # feature_counts[d,k] = count of feature d in class k |
| 26 | feature_counts = X.T.dot(Y) + smoothing |
| 27 | class_counts = Y.sum(axis=0) |
| 28 | |
| 29 | self.weights = np.log(feature_counts) - np.log(feature_counts.sum(axis=0)) |
| 30 | self.priors = np.log(class_counts) - np.log(class_counts.sum()) |
| 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 | P = X.dot(self.weights) + self.priors |
| 38 | return np.argmax(P, axis=1) |
| 39 | |
| 40 | |
| 41 | if __name__ == '__main__': |
no outgoing calls
no test coverage detected