EXTREMEXPLOIT

Gaussian Model

Apr 25th, 2020 (edited)
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.stats import multivariate_normal as multiNorm
  4.  
  5. def draw_vector(p,v):
  6.     # Plots the vector v with  origin at point p
  7.     plt.quiver(p[0],p[1], v[0], v[1])
  8.     plt.axis('equal')
  9.  
  10. # Datos Iniciales.
  11. X = list(map(np.array, ((-4,0), (2,0), (-2,6)))) # Puntos como columnas.
  12. Gaussians = [[0.8, np.array((-1,0)), np.matrix(((1,0), (0,1)))], [0.2, np.array((4,0)), np.matrix(((2,-1), (-1,2)))]]
  13. K = len(Gaussians)
  14.  
  15. # Expectation Step.
  16. R = [[], []] # [R1, R2]
  17. for k in range(K):
  18.     for x in X:
  19.         Numerator = Gaussians[k][0] * multiNorm.pdf(x, Gaussians[k][1], Gaussians[k][2])
  20.         Denominator = sum([Gaussians[l][0]*multiNorm.pdf(x, Gaussians[l][1], Gaussians[l][2]) for l in range(K)])
  21.         R[k].append(Numerator/Denominator)
  22.        
  23. print('Responsabilities:')      
  24. for k in range(K):
  25.     for n in range(len(X)):
  26.         print(f'K{k+1}N{n+1}', R[k][n])
  27.  
  28. # Maximisation Step.
  29. Xc = [x.T for x in list(map(np.matrix, ((-4,0), (2,0), (-2,6))))] # Puntos como columnas.
  30. UpdatedGaussians = [[], []]
  31.  
  32. for k in range(K):
  33.     N = sum([R for R in R[k]])
  34.     Pi = N/len(Xc)
  35.     Mean = (1/N) * sum([R[k][n]*Xc[n] for n in range(len(Xc))])
  36.     Cov = (1/N) * sum([R[k][n] * (Xc[n]-Mean) * (Xc[n]-Mean).T for n in range(len(Xc))])
  37.     UpdatedGaussians[k] = [Pi, Mean, Cov]
  38.  
  39. print('New Gaussians Parameters:', '\n')
  40. for k in range(K):
  41.     print(f'π{k+1}',UpdatedGaussians[k][0])
  42.     print(f'μ{k+1}',UpdatedGaussians[k][1])
  43.     print(f'Σ{k+1}',UpdatedGaussians[k][2])
  44.     print('\n')
  45.  
  46. # Convergence.
  47. n = sum([np.log(sum([Gaussians[k][0] * multiNorm.pdf(x, Gaussians[k][1], Gaussians[k][2]) for k in range(K)])) for x in X])
  48. print(f'Log-Likehood Value:{n}')
  49.  
  50. # Draw Models.
  51. for k in range(K):
  52.     Center = UpdatedGaussians[k][1]
  53.     Directions, Eigenvalues = np.linalg.svd(UpdatedGaussians[k][2])[0], np.linalg.svd(UpdatedGaussians[k][2])[1]
  54.     for i in range (len(Directions)): draw_vector(Center, np.squeeze(np.asarray(Directions[:,i])))
Add Comment
Please, Sign In to add comment