changeset 23:f34110e28a0a

[logistic regression] we have a working cost function
author Jeff Hammel <k0scist@gmail.com>
date Mon, 04 Sep 2017 09:14:25 -0700
parents 3713c6733990
children 89f46435a9e2
files tests/test_logistic_regression.py tvii/logistic_regression.py
diffstat 2 files changed, 5 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_logistic_regression.py	Mon Sep 04 08:59:52 2017 -0700
+++ b/tests/test_logistic_regression.py	Mon Sep 04 09:14:25 2017 -0700
@@ -21,7 +21,7 @@
 
         expected_cost = 6.000064773192205
         cost = logistic_regression.cost_function(w, b, X, Y)
-        print cost
+        assert abs(cost - expected_cost) < 1e-6
 
 if __name__ == '__main__':
     unittest.main()
--- a/tvii/logistic_regression.py	Mon Sep 04 08:59:52 2017 -0700
+++ b/tvii/logistic_regression.py	Mon Sep 04 09:14:25 2017 -0700
@@ -44,7 +44,7 @@
     """
 
     m = X.shape[1]
-    A = w.T*X  # compute activation
+    A = sigmoid(w.T*X + b)# compute activation
 
 
 def cost_function(w, b, X, Y):
@@ -58,9 +58,9 @@
     """
 
     m = X.shape[1]
-    A = w.T*X
-    cost = Y*np.log(A) + (1 - Y)*np.log(1 - A)
-    return (1./m)*cost
+    A = sigmoid(np.dot(w.T, X) + b)
+    cost = np.sum(Y*np.log(A) + (1 - Y)*np.log(1 - A))
+    return (-1./m)*cost
 
 
 def logistic_regression(_):