-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseBayesianClassifier.py
More file actions
51 lines (39 loc) · 1.76 KB
/
Copy pathBaseBayesianClassifier.py
File metadata and controls
51 lines (39 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import pandas as pd
import numpy.linalg as LA
from scipy.linalg import cholesky, solve_triangular
from scipy.linalg.lapack import dtrtri
class BaseBayesianClassifier:
def __init__(self):
pass
def _estimate_a_priori(self, y):
a_priori = np.bincount(y.flatten().astype(int)) / y.size
# Q3: para que sirve bincount?
return np.log(a_priori)
def _fit_params(self, X, y):
# estimate all needed parameters for given model
raise NotImplementedError()
def _predict_log_conditional(self, x, class_idx):
# predict the log(P(x|G=class_idx)), the log of the conditional probability of x given the class
# this should depend on the model used
raise NotImplementedError()
def fit(self, X, y, a_priori=None):
# if it's needed, estimate a priori probabilities
self.log_a_priori = self._estimate_a_priori(y) if a_priori is None else np.log(a_priori)
# now that everything else is in place, estimate all needed parameters for given model
self._fit_params(X, y)
# Q4: por que el _fit_params va al final? no se puede mover a, por ejemplo, antes de la priori?
def predict(self, X):
# this is actually an individual prediction encased in a for-loop
m_obs = X.shape[1]
y_hat = np.empty(m_obs, dtype=int)
for i in range(m_obs):
y_hat[i] = self._predict_one(X[:,i].reshape(-1,1))
# return prediction as a row vector (matching y)
return y_hat.reshape(1,-1)
def _predict_one(self, x):
# calculate all log posteriori probabilities (actually, +C)
log_posteriori = [ log_a_priori_i + self._predict_log_conditional(x, idx) for idx, log_a_priori_i
in enumerate(self.log_a_priori) ]
# return the class that has maximum a posteriori probability
return np.argmax(log_posteriori)