Spaces:
Sleeping
Sleeping
Gazara
Browse files- app.py +1 -0
- gaussian_naive_bayes.py +46 -0
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from skimage.feature import hog
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from skimage.feature import hog
|
| 4 |
+
from gaussian_naive_bayes import GNB
|
| 5 |
|
| 6 |
|
| 7 |
|
gaussian_naive_bayes.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
class GNB:
|
| 3 |
+
|
| 4 |
+
def gaussian_naive_train(self, X, y):
|
| 5 |
+
classes = np.unique(y)
|
| 6 |
+
|
| 7 |
+
model = {}
|
| 8 |
+
priors = {}
|
| 9 |
+
|
| 10 |
+
for c in classes:
|
| 11 |
+
X_c = X[y == c]
|
| 12 |
+
priors[c] = len(X_c) / len(X)
|
| 13 |
+
|
| 14 |
+
mean = np.mean(X_c, axis=0)
|
| 15 |
+
var = np.var(X_c, axis=0) + 1e-9
|
| 16 |
+
|
| 17 |
+
model[c] = (mean, var)
|
| 18 |
+
|
| 19 |
+
self.model = model
|
| 20 |
+
self.priors = priors
|
| 21 |
+
|
| 22 |
+
def predict(self, X, class_weights=None):
|
| 23 |
+
predictions = []
|
| 24 |
+
|
| 25 |
+
for x in X:
|
| 26 |
+
best_class = None
|
| 27 |
+
best_score = -float("inf")
|
| 28 |
+
|
| 29 |
+
for c in self.model:
|
| 30 |
+
mean, var = self.model[c]
|
| 31 |
+
|
| 32 |
+
score = np.log(self.priors[c])
|
| 33 |
+
|
| 34 |
+
if class_weights is not None:
|
| 35 |
+
score += np.log(class_weights[c])
|
| 36 |
+
|
| 37 |
+
log_prob = -((x - mean) ** 2) / (2 * var) - 0.5 * np.log(2 * np.pi * var)
|
| 38 |
+
score += np.sum(log_prob)
|
| 39 |
+
|
| 40 |
+
if score > best_score:
|
| 41 |
+
best_score = score
|
| 42 |
+
best_class = c
|
| 43 |
+
|
| 44 |
+
predictions.append(best_class)
|
| 45 |
+
|
| 46 |
+
return predictions
|