Created
April 10, 2020 09:09
-
-
Save tarlanahad/3eb049b0c9288b5411f93aad4b6dc3a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict(self, X): | |
y_pred = [self.__predict__(x) for x in X] | |
return y_pred | |
def __predict__(self, x): # Helper Function for the function 'predict' | |
posteriors = [] | |
for idx, c in enumerate(self.classes): | |
prior = self.priors[idx] # P(yi) | |
P_X_yi = self.__gauss_pdf__(idx, x) # P(X | yi) - Likelihood | |
# Equation 4.6 | |
posterior = 0 | |
for P_x_yi in P_X_yi: | |
posterior += np.log(P_x_yi) # P(x1 | yi) + ... + P(xn | yi) | |
posterior = posterior + np.log(prior) | |
posteriors.append(posterior) | |
return self.classes[np.argmax(posteriors)] # Select class with highest Posterior | |
def __gauss_pdf__(self, idx, x): | |
mu = self.means[idx] | |
std = self.stds[idx] | |
return np.exp(-(x - mu) ** 2 / (2 * std ** 2)) / np.sqrt(2 * np.pi * std ** 2) # Equation 4.7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment