comparison tests/test_logistic_regression.py @ 32:0f29b02f4806

[logistic regression] add model
author Jeff Hammel <k0scist@gmail.com>
date Mon, 04 Sep 2017 13:20:25 -0700
parents fa7a51df0d90
children
comparison
equal deleted inserted replaced
31:fa7a51df0d90 32:0f29b02f4806
57 57
58 def test_optimize(self): 58 def test_optimize(self):
59 """test gradient descent method""" 59 """test gradient descent method"""
60 60
61 # test examples 61 # test examples
62 w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]]) 62 w, b, X, Y = (np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]]))
63 63
64 params, grads, costs = logistic_regression.optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009, print_cost = False) 64 params, grads, costs = logistic_regression.optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009, print_cost = False)
65 65
66 # expected output 66 # expected output
67 w_expected = np.array([[0.1124579 ], 67 w_expected = np.array([[0.1124579 ],
75 self.assertAlmostEqual(params['b'], b_expected) 75 self.assertAlmostEqual(params['b'], b_expected)
76 self.assertAlmostEqual(grads['db'], db_expected) 76 self.assertAlmostEqual(grads['db'], db_expected)
77 self.compare_arrays(w_expected, params['w']) 77 self.compare_arrays(w_expected, params['w'])
78 self.compare_arrays(dw_expected, grads['dw']) 78 self.compare_arrays(dw_expected, grads['dw'])
79 79
80 def test_predict(self):
81
82 w, b, X, Y = (np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]]))
83
84 predictions = logistic_regression.predict(w, b, X)
85
86 assert predictions[0][0] == 1
87 assert predictions[0][1] == 1
80 88
81 if __name__ == '__main__': 89 if __name__ == '__main__':
82 unittest.main() 90 unittest.main()