Created
March 22, 2019 14:51
-
-
Save argaghulamahmad/544b2b18499581184607fc99f4c7c796 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class GDA: | |
| def __init__(self): | |
| self.phi = None | |
| self.sigma = None | |
| self.mu0 = None | |
| self.mu1 = None | |
| # update phi, sigma, mu0(mean 0), mu1(mean 1) here | |
| def fit(self, X, y): | |
| m = y.shape[0] | |
| cols = X.shape[1] | |
| count_y_is_zero = 0 | |
| count_y_is_one = 0 | |
| sum_x_when_y_zero = np.zeros(cols) | |
| sum_x_when_y_one = np.zeros(cols) | |
| for i in range(m): | |
| if y[i] == 0: | |
| count_y_is_zero += 1 | |
| sum_x_when_y_zero += X[i] | |
| elif y[i] == 1: | |
| count_y_is_one += 1 | |
| sum_x_when_y_one += X[i] | |
| self.phi = (1/m) * (count_y_is_one) | |
| self.mu0 = sum_x_when_y_zero / (count_y_is_zero) | |
| self.mu1 = sum_x_when_y_one / (count_y_is_one) | |
| sigma_sum = np.zeros((cols, cols)) | |
| diff = None | |
| for i in range(m): | |
| xi = X[i].reshape(1,-1) | |
| mu0 = self.mu0.reshape(1,-1) | |
| mu1 = self.mu1.reshape(1,-1) | |
| if y[i] == 0: | |
| diff = (xi - mu0) | |
| elif y[i] == 1: | |
| diff = (xi - mu1) | |
| sigma_sum += (np.dot(diff.T, diff)) | |
| self.sigma = (1/(m-1)) * sigma_sum | |
| self.mus = [self.mu0, self.mu1] | |
| # return p(y) | |
| def compute_proba_y(self, y): | |
| return self.phi**y * (1 - self.phi)**(1 - y) | |
| # return p(x|y) | |
| def compute_proba_x_given_y(self,x, y): | |
| mu = self.mu0 if y == 0 else self.mu1 | |
| n = mu.shape[0] | |
| diff = (x - mu).reshape((1,-1)) | |
| coeff = 1/((2*np.pi)**(n/2) * np.sqrt(np.linalg.det(self.sigma))) | |
| return coeff * np.exp((-1/2)*(np.matmul((diff),(np.linalg.inv(self.sigma))@(diff.T)).flatten()[0])) | |
| def predict_single(self, x): | |
| proba_y0 = self.compute_proba_y(y=0) | |
| proba_y1 = self.compute_proba_y(y=1) | |
| proba_x_given_y_0 = self.compute_proba_x_given_y(x, y=0) | |
| proba_x_given_y_1 = self.compute_proba_x_given_y(x, y=1) | |
| return 1 if proba_y1*proba_x_given_y_1 > proba_y0*proba_x_given_y_0 else 0 | |
| def predict(self, X): | |
| return [self.predict_single(x) for x in X] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment