demo.py 872 B

123456789101112131415161718192021222324252627
  1. import numpy as np
  2. m = [[0.0, 4.5], [0.0, 4.5], [11.6, 4.4], [12.2, 4.3], [1.4, 3.9], [1.4, 3.9], [2.5, 3.8], [0.1, 3.8], [0.5, 5.1], [0.7, 5.2], [0.7, 5.1], [0.0, 4.6], [0.0, 4.6]]
  3. m = np.transpose(np.array(m))
  4. xu_ = np.mean(m[1,:])
  5. yu_ = np.mean(m[0,:])
  6. xr_ = np.sqrt(np.var(m[0,:]))
  7. yr_ = np.sqrt(np.var(m[1,:]))
  8. #
  9. # -- normalizing the matrix before computing covariance
  10. #
  11. mn = np.array([list( (m[0,:]-xu_)/xr_),list( (m[1,:]-yu_)/yr_)])
  12. cx = np.cov(mn)
  13. n = m.shape[0]
  14. x = np.array([2.5,3.1])
  15. u = np.array([xu_,yu_])
  16. d = np.matrix(x - u)
  17. d.shape = (n,1)
  18. a = (2*(np.pi)**(n/2))*np.linalg.det(cx)**0.5
  19. b = np.exp(-0.5*np.transpose(d) * (cx**-1)*d)
  20. from scipy.stats import multivariate_normal
  21. xo= multivariate_normal.pdf(x,u,cx)
  22. yo= (b/a)[0,0]
  23. for row in np.transpose(m):
  24. print ",".join([str(value) for value in row])
  25. #-- We are ready to perform anomaly detection ...