Author: Not specified Language: python
Description: Not specified Timestamp: 2018-04-28 07:42:14 +0000
View raw paste Reply
def gaussian(x, mean, cov, d):
    pos = []
    for i in x:
        pos.append(np.array([i]))
    x = np.empty(pos[0].shape+(d,))
    for ind, i in enumerate(pos):
        x[:, ind] = i
    mean = np.array(mean)
    cov = np.array(cov)
    cov_det = np.linalg.det(cov)
    cov_inv = np.linalg.inv(cov)
    denom = d * np.pi * np.pi * np.sqrt(cov_det)
    diff = x - mean
    mahalanobis = np.einsum('...k,kl,...l->...', diff, cov_inv, diff)
    return (np.exp(-mahalanobis/2)/denom)[0]

def likelihood_classify(x, mean_arr, cov_arr):
    gaussian_arr = []
    for i in range(5):
        gaussian_arr.append(gaussian(x, mean_arr[i], cov_arr[i]))
    return gaussian_arr.index(max(gaussian_arr))

def posterior_classify(x, mean_arr, cov_arr, posterior_arr):
    gaussian_arr = []
    for i in range(5):
        gaussian_arr.append(posterior_arr[i]*gaussian(x, mean_arr[i], cov_arr[i]))
    return gaussian_arr.index(max(gaussian_arr))

   
View raw paste Reply