def test_sigmoid_function2(self): X_1 = np.array([[1200000]]) X_2 = np.array([[-25000]]) X_3 = np.array([[4, 5, 6]]) X_4 = magic(3) X_5 = np.eye(2) answer_1 = np.array([[1]]) answer_2 = np.zeros((1, 1)) answer_3 = np.array([[ 0.9820, 0.9933, 0.9975]]) answer_4 = np.array([[0.9997, 0.7311, 0.9975], [0.9526, 0.9933, 0.9991], [0.9820, 0.9999, 0.8808]]) answer_5 = np.array([[0.7311, 0.5000], [0.5000, 0.7311]]) np.testing.assert_array_equal(sigmoid_function(X_1), answer_1) np.testing.assert_array_equal(sigmoid_function(X_2), answer_2) np.testing.assert_array_almost_equal(sigmoid_function(X_3), answer_3, decimal=3) np.testing.assert_array_almost_equal(sigmoid_function(X_4), answer_4, decimal=3) np.testing.assert_array_almost_equal(sigmoid_function(X_5), answer_5, decimal=3)
def test_sigmoid_gradient(self): actual = sigmoid_gradient(np.vstack((np.array([[-1., - 2., - 3.]]), magic(3)))) expected =np.array([[1.9661e-001, 1.0499e-001, 4.5177e-002], [3.3524e-004, 1.9661e-001, 2.4665e-003], [4.5177e-002, 6.6481e-003, 9.1022e-004], [1.7663e-002, 1.2338e-004, 1.0499e-001]]) np.testing.assert_almost_equal(actual, expected, decimal=3)
def test_cost_function1(self): X = np.c_[np.ones((3, 1)), magic(3)] y = np.array([[1, 0, 1]]).T theta = np.array([[-2, -1, 1, 2]]).T j, grad = cost_function(X, y, theta) answer_j = 4.6832 answer_grad = np.array([[0.31722, 0.87232, 1.64812, 2.23787]]).T np.testing.assert_almost_equal(j, answer_j, decimal=3) np.testing.assert_array_almost_equal(grad, answer_grad, decimal=3)
def test_cost_function_reg1(self): X = np.c_[np.ones((3, 1)), magic(3)] y = np.array([[1, 0, 1]]).T theta = np.array([[-2, -1, 1, 2]]).T lambda_ = 3 j, grad = cost_function_reg(X, y, theta, lambda_) answer_j = 7.6832 answer_grad = np.array([[0.31722, -0.12768, 2.64812, 4.23787]]).T np.testing.assert_almost_equal(j, answer_j, decimal=3) np.testing.assert_array_almost_equal(grad, answer_grad, decimal=3)
def test_opt_one_vs_all(self): X = np.r_[magic(3), np.sin(np.arange(1, 4)).reshape(1, 3), np.cos(np.arange(1, 4)).reshape(1, 3)] y = np.array([[1, 2, 2, 1, 3]]) y = y.T num_labels = 3 theta = np.ones((X.shape[1] + 1, 1)) * 0.1 actual = ex3.opt_one_vs_all(X, y, theta, num_labels) expected = np.array([[-0.559478, 0.619220, -0.550361, -0.093502], [-5.472920, -0.471565, 1.261046, 0.634767], [0.068368, -0.375582, -1.652262, -1.410138]]) np.testing.assert_array_almost_equal(actual, expected, decimal=3)