Commits

Miki Tebeka committed 26252e5

found threshold

Comments (0)

Files changed (1)

 
 
 def find_threshold(fn):
-    raw = loadmat('ex8/ex8data2.mat')
+    raw = loadmat('ex8/ex8data1.mat')
     X = raw['Xval']
     y = raw['yval'].ravel()
 
-    dist = np.fromiter((fn(x) for x in X), float)
+    dists = np.fromiter((fn(x) for x in X), float)
 
     best_f = 0
     best_t = 0
-    for t in np.linspace(dist.min(), dist.max(), 100):
-        fn = np.vectorize(lambda x: 0 if fn(x) < t else 1)
-        f = fbeta_score(y, fn(X), 1.)
+    for t in np.linspace(dists.min(), dists.max(), 100):
+        preds = (dists > t).astype(int)
+        f = fbeta_score(y, preds, 1.)
         if f > best_f:
             best_f = f
             best_t = t
 
 
 
-if __name__ == '__main__':
-    #anomaly()
-    #raw_input()
+def show_threshold():
 
     data = loadmat('ex8/ex8data1.mat')
     X = data['X']
 
-    #cov = MinCovDet().fit(X)
-    cov = EmpiricalCovariance().fit(X)
-    fn = lambda x: abs(cov.score(x))
-    t, f = find_threshold(fn)
-    print(t, f)
+    cov = MinCovDet().fit(X)
+    #cov = EmpiricalCovariance().fit(X)
+    def dist(x):
+        return abs(cov.score(x))
+    t, f = find_threshold(dist)
+    print('threshold: {}\nfscore: {}'.format(t, f))
+
+    fn = cov.score
+    xs, ys, z = calc_contour(X, fn)
+
+    plt.scatter(X[:,0], X[:,1], marker='x')
+    plt.contour(xs, ys, z)
+    oxs, oys = [], []
+    for x in X:
+        if abs(cov.score(x)) > t:
+            oxs.append(x[0])
+            oys.append(x[1])
+    plt.scatter(oxs, oys, marker='o', color='red')
+
+    plt.grid()
+    plt.show()
+
+if __name__ == '__main__':
+    #anomaly()
+    show_threshold()
+    raw_input()