Commits

Miki Tebeka committed 81683c3

using scikit-learn

  • Participants
  • Parent commits 775699c

Comments (0)

Files changed (2)

 import numpy as np
 import matplotlib.pyplot as plt
 from scipy.io import loadmat
+from sklearn.covariance import EmpiricalCovariance, MinCovDet
 
 
 def p1(x, var, mue):
 
     return xs, ys, z
 
+def anplot(data, fn):
+    xs, ys, z = calc_contour(data, lambda x: fn(x))
+
+    plt.scatter(data[:,0], data[:,1], marker='x')
+    exps = np.arange(-20, -1, 3)
+    fn = np.vectorize(lambda n: 10**n)
+    plt.contour(xs, ys, z) #, fn(exps))
+    plt.grid()
+    plt.show()
+
+
 def anomaly():
     data = loadmat('ex8/ex8data1.mat')
     train = data['X']
 
     mue = train.mean(0)
     var = train.var(0)
-    xs, ys, z = calc_contour(train, lambda x: p(x, mue, var))
-    z *= 10
+    fn = lambda x: p(x, mue, var)
+    anplot(train, fn)
 
-    plt.scatter(train[:,0], train[:,1], marker='x')
-    exps = np.arange(-20, -1, 3)
-    fn = np.vectorize(lambda n: 10**n)
-    plt.contour(xs, ys, z, fn(exps))
-    plt.show()
+
+def anomaly_skl():
+    data = loadmat('ex8/ex8data1.mat')
+    X = data['X']
+
+    cov = MinCovDet().fit(X)
+    fn = lambda x: cov.mahalanobis([x])
+    anplot(X, cov.score)
 
 
 if __name__ == '__main__':

File ex8.pyc

Binary file removed.